在 docker 容器内公开 运行 的 SSH 隧道端口

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

在 docker 容器内,我在交互式 shell 中创建了以下隧道:

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

在同一个容器的另一个 shell 中,我可以成功 运行 以下操作:

curl http://localhost:8443

服务器 (remotehost.com) 确实以 HTML 内容响应。

(注意:我现在使用的是纯HTTP,以便于调试。最后我需要使用HTTPS,这就是为什么我选择本地端口为8443。)

这个 docker 容器确实公开了它的端口 8443:

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

但是当我尝试从主机连接到该端口时,我得到以下信息:

# 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

我迷路了。为什么它的行为与从容器内部连接时的行为不完全相同?我对 SSH 隧道有什么误解吗?

解决方案是将 -g 标志添加到创建隧道的 ssh 行。