0

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

rogger2016
  • 821
  • 3
  • 11
  • 28

1 Answers1

2

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.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • You pointed me in the correct direction :), So the solution was to start the application on a test profile and set the baseUrl of appb appb:8081(service-name-defined in compose file):8081 – rogger2016 Nov 07 '20 at 09:10