On the host I check connectivity to the serivice via the port:
telnet 127.0.0.1 30305
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Now I want to connect from inside a container…
I start the container like this:
docker run -it --add-host james:host-gateway --rm alpine /bin/ash
then install telnet like this:
apk update
apk add busybox-extras
check that the special host-ip resolves, like this:
ping james
PING james (172.17.0.1): 56 data bytes
64 bytes from 172.17.0.1: seq=0 ttl=64 time=0.082 ms
however:
telnet james 30305
Simply hangs forever
What can be the issue?
I know it is a Docker networking issue, because I ran the 30305 service on a totally different server, and it connects fine, the issue is ONLY when the service is running on the SAME machine as Docker.
I’m assuming this special Docker host-ip trick is all local and doesn’t touch the firewall.
If you’re using Docker Desktop, host.docker.internal should work
Not sure about pure Docker Engine
But if you want to access a Docker service from another, you don’t need to do that through the host machine - You also don’t need to map the ports at all!
Simply put both containers in a shared network, and use the container/service name
In such a stack, the frontend service could reach the backend using backend or app-back as the hostname
e.g. http://backend:3000, http://app-back, etc.
In your post you use telnet to connect to a loopback interface on the host and want to connect to a port on another IP address from a container. That would never work if the service on the host listens only on localhost, not even without Docker. It is just that the IP address you use for connecting from a container is a Docker network gateway. You need the process on the host listen on the gateway IP as well, or you can implement something similar to Docker Desktop’s host.docker.internal
Nowhere in @deanayalon’s post was mentioned that you used that But it is true, that with Docker Desktop that would be the solution so the requests from the containers would be forwarded to the loopback IP on the host. Without that, you have to implement it for youself as I described in the linked post. Or depending on from where you need to access the service on the host, you can just reconfigure it to listen on the docker network gateway.