Docker - OS X forward localhost 22 port to contain

2019-01-27 01:16发布

问题:

I'm using Docker on my Mac OSX.

I have a container with an open ssh daemon, listening to port 22.

The virtual machine is set to forward all traffic of port 2022 of the machine to this container 22 port.

I want to set my localhost to forward all 2022 traffic to the machine 2022 port.

My machine runs at 192.168.99.100

I already forwarded all the HTTP traffic at localhost port 8080 to the machine using Apache virtual host configuration and Proxy.

What is the right way to do the same for ssh?

I think it involve SSH Tunnelling as mentioned here but I don't understand how I can set that my localhost will always forward ssh connections from localhost:2022 to 192.168.99.100:2022

p.s. I know docker container should not have SSH installed, but I need it for testing proposes.

回答1:

now I want to connect between my localhost:2022 to the machine:2022

Then you need your boot2docker VM (created by docker-machine) to port-forward the port 2022 to your MacOS host.
See "docker nginx container not receiving request from outside, connection refused" as an example.

VBoxManage controlvm "default" natpf1 "tcp-port2022,tcp,,2022,,2022"
VBoxManage controlvm "default" natpf1 "udp-port2022,udp,,2022,,2022"

Note: this is not releated to ssh specifically, it is only related to the fact that you are using a VM as a Linux host, and you have mapped something to its port 2022.
That port will be visible from your actual MacOS localhost only if you port-forward it.