Connecting to a port on the host from inside the container

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

For example:

services:
  frontend:
    container_name: app-front
    ...
  backend: 
    container_name: app-back
    ...

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.

nowhere in my question did i use host.docker.internal

and nowhere did i say i wanted to access from another container.

feels like you totally misread my question.

Sorry if I understood wrong, but did you not want to connect to your host machine from a container? That is done via host.docker.internal

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 :slight_smile: 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.

Seems a different solution was used

https://www.reddit.com/r/docker/s/BptDKoufdZ

1 Like

Then I will leave this UFW related documentation link here too

https://docs.docker.com/engine/network/packet-filtering-firewalls/#docker-and-ufw