dockerhttpconnection-refused

HTTP connection refused between two docker containers, whereas from outside both connectable


Try to make a REST request between 2 docker-containers.

To be more explicit, I have 2 images of SpringBoot REST-services (A and B), A is calling B via HTTP.

Issue

When I run them locally (e.g. in IDE) they communicate fine.

But when I run them in Docker, the A can't connect to the B. When I use Postman though, I can connect to A and B.

Docker compose

version: "3.9"
services:
  human_cloning_facilities:
    image: erwanlt94/human_cloning_app:6.0.0
    container_name: human_cloning_app
    restart: unless-stopped
    ports:
      - "8080:8080"

  jedi_service:
    image: erwanlt94/jedi_api:1.0.0
    container_name: jedi_service
    restart: unless-stopped
    ports:
      - "8082:8082"

volumes:
  human_cloning_facilities:
    driver: local
  jedi_service:
    driver: local

Log trace

When I call the endpoint on A that will connect to B:

2022-06-21 14:44:55.725 ERROR [Human cloning,b8d7c7a112fd94d0,b8d7c7a112fd94d0] 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.RetryableException: Connection refused (Connection refused) executing GET http://127.0.0.1:8082/jedis] with root cause
2022-06-21T14:44:55.727148588Z 
2022-06-21T14:44:55.727162378Z java.net.ConnectException: Connection refused (Connection refused)
2022-06-21T14:44:55.727168574Z  at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
2022-06-21T14:44:55.727174195Z  at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
2022-06-21T14:44:55.727179444Z  at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
2022-06-21T14:44:55.727184823Z  at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
2022-06-21T14:44:55.727281097Z  at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
2022-06-21T14:44:55.727295172Z  at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177) ~[na:na]
2022-06-21T14:44:55.727324103Z  at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[na:na]
2022-06-21T14:44:55.727330887Z  at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[na:na]
2022-06-21T14:44:55.727336098Z  at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:na]
2022-06-21T14:44:55.727353809Z  at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[na:na]
2022-06-21T14:44:55.727358566Z  at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[na:na]
2022-06-21T14:44:55.727363344Z  at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1253) ~[na:na]
2022-06-21T14:44:55.727367999Z  at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187) ~[na:na]
2022-06-21T14:44:55.727373095Z  at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081) ~[na:na]
2022-06-21T14:44:55.727378075Z  at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015) ~[na:na]
2022-06-21T14:44:55.727383009Z  at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1592) ~[na:na]
2022-06-21T14:44:55.727391083Z  at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520) ~[na:na]
2022-06-21T14:44:55.727396501Z  at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527) ~[na:na]
2022-06-21T14:44:55.727401416Z  at feign.Client$Default.convertResponse(Client.java:108) ~[feign-core-10.12.jar!/:na]
2022-06-21T14:44:55.727406394Z  at feign.Client$Default.execute(Client.java:104) ~[feign-core-10.12.jar!/:na]
2022-06-21T14:44:55.727411293Z  at org.springframework.cloud.sleuth.instrument.web.client.feign.TracingFeignClient.execute(TracingFeignClient.java:79) ~[spring-cloud-sleuth-instrumentation-3.1.2.jar!/:3.1.2]
2022-06-21T14:44:55.727416481Z  at org.springframework.cloud.sleuth.instrument.web.client.feign.LazyClient.execute(LazyClient.java:54) ~[spring-cloud-sleuth-instrumentation-3.1.2.jar!/:3.1.2]
2022-06-21T14:44:55.727421646Z  at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-10.12.jar!/:na]
2022-06-21T14:44:55.727426635Z  at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.12.jar!/:na]
2022-06-21T14:44:55.727431758Z  at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.12.jar!/:na]
2022-06-21T14:44:55.727436913Z  at com.sun.proxy.$Proxy189.findAllUsingGET(Unknown Source) ~[na:na]
2022-06-21T14:44:55.727441948Z  at com.erwan.human.controller.HumanCloningController.getAllJedi(HumanCloningController.java:128) ~[classes!/:6.0.0-SNAPSHOT]

What is the problem?


Solution

  • It sounds like you're using localhost as the host name when you try to connect.

    In a Docker context, localhost is the container itself. You can connect to another container by using it's service name as it's host name.

    So when connecting from human_cloning_facilities to jedi_service, you should use http://jedi_service:8082/ and when connecting from jedi_service to human_cloning_facilities, you should use http://human_cloning_facilities:8080/.

    When connecting between containers on the bridge network that Docker creates, you should use the container ports. That doesn't make a difference in your case since you map your ports to the same port numbers on the host. But if you only need to access the containers from other containers on the network, you don't need to map the ports to host ports. That's only needed if you need to access the containers from the host.