如何从 Docker 容器内部访问主机 ssh 隧道?

如何从 Docker 容器内部访问主机 ssh 隧道?

我正在创建一个 ssh 隧道,并尝试从 docker 容器内部访问它。

在客户端上创建 ssh 隧道

ssh -R 0.0.0.0:8088:localhost:8088 [email protected] -N

在主机/服务器上

# The tunneled connection is returning the expected page
curl localhost:8088
# Another service running directly on the host/server (not in docker container) which also works fine
curl localhost:8081 

在 docker 容器内

# Create the docker container
docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash

# The tunneled connection will fail
curl localhost:8088
# > curl: (7) Failed to connect to 172.17.0.1 port 8088 after 0 ms: Connection refused

# While the other service if accessible perfectly fine
curl localhost:8081 

如何使隧道连接可供 Docker 容器使用?

答案1

您需要在远程服务器上添加 或 。否则,sshGatewayPorts clientspecified命令中的 将被忽略,并且端口将仅绑定到环回接口。GatewayPorts yes/etc/ssh/sshd_config0.0.0.0

这里还有更多信息和示例:https://www.ssh.com/academy/ssh/tunneling-example#remote-forwarding

sudo systemctl restart sshd改变之后别忘记这么做。

相关内容