Docker Community Forums

Share and learn in the Docker community.

Connection refused on API request between containers with docker compose

I’m developing a multi-container Docker application and I want a container to make an HTTP request to API of other container using Docker Compose. I’m getting Connection Refused error.

Both containers run ASP.NET Core 2.2. The IDE I’m using is Visual Studio 2017. I’m testing API calls with Postman.

Things I’ve tried:
:heavy_check_mark: Ports are exposed in Dockerfiles
:heavy_check_mark: Ports are declared in Docker Compose configuration
:heavy_check_mark: Network is declared in Docker Compose and containers are connected to it
:heavy_check_mark: Http request URI uses service name and not localhost or local IP address
:heavy_check_mark: Http request URI uses container port (80) and not host port
:heavy_check_mark: Firewall is disabled

Dockerfiles

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
...

Docker Compose configuration:

version: '3.4'

services:
  servicea:
...
ports:
  - "51841:80"
  - "44364:443"
networks:
  - local

  serviceb:
  ...
ports:
  - "65112:80"
  - "44359:443"
networks:
  - local

networks:
  local:
driver: bridge

serviceA controller action:

[Route("[action]")]
[HttpGet]
public IActionResult foo()
{
   HttpClient client = _clientFactory.CreateClient();

   var result = client.GetAsync("http://serviceb:80/api/bar").Result;
   var response = result.Content.ReadAsStringAsync().Result;

   return new OkObjectResult(response);
}

If I do an Http Get request to serviceA (with host port 51841) with Postman at http://localhost:51841/api/foo I’d like to get the response of serviceB’s Bar action.

But I’m getting Connection refused

Raw exception details:

System.Net.Http.HttpRequestException: Connection refused ---> System.Net.Sockets.SocketException: Connection refused
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)