Home > Software engineering >  Docker container allows me to curl hosts port 80 , but not other port
Docker container allows me to curl hosts port 80 , but not other port

Time:08-04

SO i have a service running in my localhost:80 , and localhost:8080

I need to fetch it from within a docker container

So I enter inside my docker container cli and try the following commands.

curl http://www.example.com //a famous website --> works
curl 172.17.0.1 ---> works , this is fetching my hosts localhost port 80 , its the default docker interface and resolves on the host
curl 172.17.0.1:1112 ---> I can fetch this , i have a simple express server running there returning a hello world in my local machine , it can also be curled from withing the host with a curl localhost:1112

Above as you can see im using 172.17.0.1 to connecto to my host from within my container, and not localhost, because localhost would mean the local connection of said container, and thats not what im looking for.

Now the issue comes with the following.

I crate a ssh tunnel in my port 8888 to another machine, which can only be accessed using a vpn that is running in my host. With the following command

ssh -fN [email protected] -L 8888:db.env.prise:80

This creates a tunnel that I can curl in my host machine localhost:8888 If I try this from within my host

curl -s http://localhost:8888/test | jq

It correctly fetches a json. SO the idea is to do something similar from within my container. I go ahead to my container and type

curl http://172.17.0.1:8888/test
Failed to connect to 172.17.0.1 port 8888: Connection refused

And thats the eerror that I receive.

Why can I fetch every single port except that one? I suspect it might have something to do with my docker not being in the vpn ¿?

HOw can I fix this.

I have a openvpn file for the connection but thats it.

Altho I dont really think its the vpns fault, because if I Curl from my localhost with the vpn disconnected, the curl will fail but at least it attempts to curl it being stuck for a while. WHile trying to curl that port from within the docker instantly gets rejected.

CodePudding user response:

It looks like you are attempting to connect from docker to a resource that you can only access via SSH on your host.

A valid solution to this problem would be to forward the port of the external machine to your machine via:

ssh -fN [email protected] -L 8888:db.env.prise:80

This will redirect the external port to a local port. The problem is that docker cannot access this port.

With socat, you can open a new port that listens on all interfaces and not just on local:

socat TCP-LISTEN:8889,fork,bind=0.0.0.0.0 TCP:localhost:8888

Through this port, connections will be redirected to your target machine:

8889->8888->80

  • Related