Docker Community Forums

Share and learn in the Docker community.

Inter container communication with same network

Hi all, I am learning about inter container communication.

Here is my scenario. I have two apps created.
App1 - It has the Default weatherforecast controller.
App2 - It has a valuescontroller that will call the weatherforecast controller of App1 and return the vlaues.

To accomplish this, I created a custom network where both the container will be deployed.

cmd : docker network create example
cmd : docker run --net example -p 7015:80 <weatherforecast_img> --name container1
cmd : docker run --net example -p 7020:80 <values_img> --name container2

Both the containers started successfully.

My values controller Get method looks like this.

[HttpGet]

    public string Get()
    {
        string weatherData = string.Empty;
        try
        {
            using var client = new HttpClient
            {
                BaseAddress = new Uri("http://container1.example/")
            };
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var response = client.GetAsync("weatherforecast").Result;
            if (response.IsSuccessStatusCode)
            {
                var jsonTask = response.Content.ReadAsStringAsync();
                jsonTask.Wait();
                weatherData = jsonTask.Result;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return weatherData;
    }

In the line where I frame the base address, container1 refers to the name of the first container that producer data and example refers to the network that the container1 users.

with this piece of code in my second app, I build the image and deploy the second container as below.
docker run --net example -p 7020:80 <values_img> --name container2

both the services are starting successfully. but when I hit the browser with the URL “http://localhost:7020/values”, I get the below error.

when I normally run the app without containers with just localhost:5000 as the base address, then hte app works perfectly normal.

I was following this link for this sample.

what am I missing when I deploy in containers.

Thanks,

1 Like