rogger2016
rogger2016

Reputation: 939

Springboot WebClient Broken in docker container

Iv created two Springboot applications that Iv dockerized and created local containers. When I run the applications locally through intellij on my machine they work ok. Application A, on localhost:8080 has a Spring WebClient with a baseUrl localhost:8081 configured to call Application B running on port 8081. This works great.

The problem starts when I add those container to a docker compose file and spin then up

version: "3.7"
services:

appa:
  image: application/myapp:1
  hostname: localhost
  ports:
    - 8080:8080

  appb:
    image: application/myapp:2
    hostname: localhost
    ports:
      - 8081:8081

I can hit localhost:8080 from the browser, but when the client in the application tried to call application b using the WebClient, it falls over with

Error has been observed at the following site(s):
             |      |_ checkpoint ⇢ Request to GET 
http://localhost:8081/api/feed [DefaultWebClient]
             | Stack trace:
             | Caused by: java.net.ConnectException: finishConnect(..) failed: 
Connection refused

I can hit both apps from the browser or curl, but it seems they cant communicate internally inside the docker containers

Any help appreciated

Upvotes: 0

Views: 1315

Answers (1)

Andy Wilkinson
Andy Wilkinson

Reputation: 116091

If you are using localhost inside one container to try to communicate with a service running inside another container, that's the wrong host to use. localhost inside a container refers to the container itself not its host. To establish a connection between containers, you'll need to use the IP address of the container that you want to connect to rather than localhost. This networking tutorial may be of interest.

Upvotes: 2

Related Questions