1

I am developing a Spring Boot application using IntelliJ. This application depends on several other applications that are deployed in docker containers.

As there is a lot of communication going on, I need to enable an application inside docker to connect to my application running on localhost.

I have seen lots of people say you need to use host.docker.internal but this didn't work. What I have tried:

  • Configuring my local application to bind to 0.0.0.0 via server.address: 0.0.0.0
  • Configuring my docker container with extra_hosts=host.docker.internal:host-gateway
  • Putting 127.0.0.1 host.docker.internal in my hosts file

Nonetheless nothing worked, I'm always getting Connection refused when trying to reach my local application from within docker.

As I'm working in a team, I need a solution that works out of the box for everyone, so no solution tied to my specific computer. Because I have seen people using the IP of their local machine and stuff in order to make it work. So hostname resolution host.docker.internal would be ideal - if it worked..

I have browsed lots of topics on stackoverflow but none of the answers worked for me, so thats why I decided to create a new question.

I am using:

  • Windows 10 Version 21H2 Build 19044.2251
  • Ubuntu 20.04 WSL2
  • Docker version 20.10.22, build 3a2c30b
Manu
  • 284
  • 2
  • 20
  • You should not be using `host.docker.internal` to connect between containers. Create a Docker network and use Docker's DNS system. If you're using Docker Compose, this is set up automatically, and [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation has some details on it. – David Maze Aug 23 '23 at 10:43
  • (Remember that each container has its own isolated network environment, and each container believes `localhost` or `127.0.0.1` is itself. Saying a process is "on localhost" is trivially true and doesn't mean anything. But it sounds like one of these processes is on the host system outside a container.) – David Maze Aug 23 '23 at 10:44
  • @David yeah, my fault. By "localhost" im referrring to the docker host, so my machine. Unfortunately Im not using docker compose, i wrote my own script to deploy the containers. But doesn't Dockers DNS System assign a new IP to the containers every time they are redeployed? I would need a fixed IP in order to make this work, right? – Manu Aug 23 '23 at 12:16
  • You can `docker network create` a network if you're not using Compose; for example [How to reach docker containers by name instead of IP address?](https://stackoverflow.com/questions/31149501/how-to-reach-docker-containers-by-name-instead-of-ip-address) you should never need to set or know the container-internal IP addresses. – David Maze Aug 23 '23 at 13:16
  • How would this enable my application deployed in docker to communicate with my local application? Docker networks are only visible within docker, right? – Manu Aug 24 '23 at 07:45

0 Answers0