Search code examples
dockerssh-tunnel

Expose port of SSH tunnel that is running inside a docker container


Inside a docker container I create the following tunnel in an interactive shell:

ssh -4 root@remotehost.com -L 8443:127.0.0.1:80

In another shell on the same container I can successfully run the following:

curl http://localhost:8443

The server (remotehost.com) does respond with HTML content.

(Note: I'm using plain HTTP for now to make it easier to debug. In the end I need to be using HTTPS, that's why I choose the local port to be 8443.)

This docker container does expose its port 8443:

# docker port be68e57bc3e0
8443/tcp -> 0.0.0.0:8443

But when I try to connect from the host to that port I get the following:

# curl --verbose http://localhost:8443
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8443 (#0)
> GET / HTTP/1.1
> Host: localhost:8443
> User-Agent: curl/7.64.1
> Accept: */*
> 
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
* Closing connection 0

Here I'm lost. Why doesn't it behave exactly the same way as when connecting from inside the container? Am I misunderstanding something about SSH tunnels?


Solution

  • The solution was to add the -g flag to the ssh line that creates the tunnel.