Address not available in Docker

  .net, asp.net-core, async-await, c#, docker

I have a project A. I’m trying to request with the following line of code:

return await _httpClient.PostAsync(requestUri, content);

The requestUri I call is something simple: http://localhost:3036/api. This URL is from a docker container (project B).

When I run this program (project A) running locally on my machine debugging in visual studio or in IIS, it works perfectly. It calls project B’s URL, executes the action and then returns.

But when I put project A running inside a container, inside a Docker Compose with project B running in another container, I get the following error:

Message: Address not available

Stacktrace:

at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)

at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)

at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)

at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

And the URL of project B that I call (http://localhost:3036/api) is still the same, working.
I test it inside Postman, and it returns ok.
But when project A running inside the container tries to call this URL, it gets the error: "Address not available".

And I don’t change anything in the project.
Project A outside the container works.
Project A inside the container doesn’t work.

What could it be?

A Image to help understand:
enter image description here

Source: Docker Questions

LEAVE A COMMENT