Search code examples
.netdockercontainers

"Connection Refused" Http calls between dotnet controllers running on docker containers


I have two Dotnet controller APIs

  1. dashboard-services
  2. config-services

I am running these controllers as docker images with the following commands:

  • docker run -d -p 7702:80 --network=bridge config-services
  • docker run -d -p 7802:8080 --network=bridge dashboard-services

I can successfully make calls to each of these services individually at http://localhost:7702 and http://localhost:7802

My issue is that when I make a call that results in postman > dashboard-services > config-services, the call fails with a System.Net.Http.HttpRequestException: Connection refused (localhost:7702) // System.Net.Sockets.SocketException (111): Connection refused. It appears that my images have no problem communicating with localhost, but do have a problem communicating with eachother. My understanding is that this should be resolved by placing both of these images in the same network, which I am specifying with --network=bridge.

Why am I hitting this error?


Solution

  • If I understand your problem correctly, then - localhost:

    In computer networking, localhost is a hostname that refers to the current computer used to access it. The name localhost is reserved for loopback purposes.

    When you run the app in docker container localhost means the container itself, not the host machine, so for dashboard-services container it will not have anything running on the 7702 port (i.e. the config-services).

    My understanding is that this should be resolved by placing both of these images in the same network

    AFAIK even if you don't specify the network the default should work. But you need to use the container name and internal port, so dashboard-services should call config-services:80 (on Windows you can also use host.docker.internal instead of localhost, so host.docker.internal:7702 should also work).

    Also please check out: