0

I have a Spring Boot config server running in localhost on port 8888. I'd like to access this service from inside a docker container. I cannot put this service inside the same docker-compose and in the same network.

Service: http://localhost:8888/cpo-executor/dev
Using my ip: http://192.168.0.6:8888/cpo-executor/dev

I've checked this address on browser and it's working. When I try to access from inside a docker container I got an error:

docker exec -it 7febe846f2ea /bin/bash
curl http://192.168.0.6:8888/cpo-executor/dev

Error:

curl: (7) Failed to connect to 192.168.0.6 port 8888: Connection timed out

I've tried to start my containers putting "network_mode: host" in docker-compose but I ended up falling into another error, from one container not communicating with another.

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Caused by: java.net.UnknownHostException: mysql

How can I access a service from host from inside a docker container?

Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38

1 Answers1

0

The solution depends on what platform your Docker engine is running on.

  1. On Windows, your containers can connect to host with special hostname host.docker.internal (see Docker docs). By contrast, host network mode does not work on Windows (see Docker docs).

  2. On Linux, you should use host network mode. However, this will break how your containers connect to each other. In the default bridge network mode they can connect to each other by using hostname identical to the name of service (mysql in your case). With Host network mode however you should use localhost hostname instead, because all containers share the same host's networking.

user14967413
  • 1,264
  • 1
  • 6
  • 12