从Docker容器访问主机的ssh隧道

问题描述:

使用ubuntu tusty,在远程计算机上运行了一项服务,我可以通过SSH隧道通过端口转发从 localhost:9999 访问该服务。

Using ubuntu tusty, there is a service running on a remote machine, that I can access via port forwarding through an ssh tunnel from localhost:9999.

我正在运行一个Docker容器。我需要通过主机的隧道从容器内部访问该远程服务。

I have a docker container running. I need to access that remote service via the host's tunnel, from within the container.

我尝试使用 -L从容器到主机的隧道9000:host-ip:9999 ,然后从容器内部通过 127.0.0.1:9000 访问服务失败。要检查端口映射是否打开,我尝试了

nc -luv -p 9999#在主机
nc -luv -p 9000#在容器

I tried tunneling from the container to the host with -L 9000:host-ip:9999 , then accessing the service through 127.0.0.1:9000 from within the container fails to connect. To check wether the port mapping was on, I tried nc -luv -p 9999 # at host nc -luv -p 9000 # at container

跟随此,parag。 2 ,但即使在执行

nc -luv host-ip -p 9000
时,也没有感知到的通信容器

following this, parag. 2 but there was no perceived communication, even when doing nc -luv host-ip -p 9000 at the container

我还尝试通过 docker run -p 9999:9000 映射端口,但这报告了绑定失败,因为主机端口已在使用(大概是从主机隧道到远程计算机)。

I also tried mapping the ports via docker run -p 9999:9000 , but this reports that the bind failed because the host port is already in use (from the host tunnel to the remote machine, presumably).

所以我的问题是

1-如何实现连接?我是否需要设置一条通往主机的ssh隧道,还是可以仅通过docker端口映射来实现?

1 - How will I achieve the connection? Do I need to setup an ssh tunnel to the host, or can this be achieved with the docker port mapping alone?

2-测试连接的快速方法是什么起来了吗

2 - What's a quick way to test that the connection is up? Via bash, preferably.

谢谢。

我认为你可以通过在您的docker run中添加-net = host 来实现。但也请参见以下问题:将主机端口转发到Docker容器

I think you can do it by adding --net=host to your docker run. But see also this question: Forward host port to docker container