简体   繁体   English

docker-machine中的端口转发?

[英]Port forwarding in docker-machine?

Since boot2docker is deprecated I've switched to docker-machine but I don't know how to open a port from docker-machine . 由于boot2docker已弃用,我已切换到boot2docker docker-machine但我不知道如何从docker-machine打开端口。 In boot2docker I could do like this: boot2docker我可以这样做:

boot2docker ssh -L 27017:localhost:27017

This would forward port 27017 from VirtualBox to localhost 27017 as long as the SSH connection is open. 只要SSH连接打开,这就会将端口27017从VirtualBox转发到localhost 27017。 Note that I'm not looking for a way to open the port permanently in VirtualBox. 请注意,我不是在寻找一种在VirtualBox中永久打开端口的方法。 How can I achieve this with docker-machine ? 如何使用docker-machine实现这一目标?

You can still access the VBoxmanage.exe command from the VirtualBox used by docker machine: 您仍然可以从docker机器使用的VirtualBox访问VBoxmanage.exe命令:

VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port27017,tcp,,27017,,27017";
  • Use docker-machine info to get the name of your vm. 使用docker-machine info获取vm的名称。
  • use modifyvm if the vm isn't started yet. 如果vm尚未启动,请使用modifyvm

See a practical example in this answer . 请参阅此答案中的实际示例。


That is the current workaround, pending the possibility to pass argument to docker-machine ssh : see issue 691 . 这是当前的解决方法,等待将参数传递给docker-machine ssh的可能性:参见问题691

The other workaround is to not forward port, and use directly the IP of the VM: 另一种解决方法是转发端口,直接使用VM的IP:

 $(docker-machine ip default)

As commented by sdc : sdc 评论

You can confirm that port forwarding is set up correctly with 您可以使用确认正确设置端口转发

 VBoxManage showvminfo boot2docker-vm | grep "NIC.* Rule" 

With recent versions of machine, you can simply do (where default is the name of the machine): 使用最新版本的机器,您可以简单地执行( 默认为机器名称):

docker-machine ssh default -L 27017:localhost:27017

This is a more temporary solution than the VM configuration change. 这是比VM配置更改更临时的解决方案。

Use the following variation to only forward ports in a background process: 使用以下变体仅在后台进程中转发端口:

docker-machine ssh default -f -N -L 27017:localhost:27017
  • -f Requests ssh to go to background just before command execution. -f请求ssh在命令执行之前转到后台。
  • -N Allow empty command (useful here to forward ports only) -N允许空命令(此处仅用于转发端口)

You can ssh into the machine and pass on the regular port forwarding arguments: 您可以ssh进入计算机并传递常规端口转发参数:

ssh docker@$(docker-machine ip default) -L 27017:localhost:27017

The password of the docker user is tcuser . docker用户的密码是tcuser (see https://github.com/boot2docker/boot2docker ) (见https://github.com/boot2docker/boot2docker

Since I have hard time remembering how to do this I've created a small bash script called pf (which stands for "port forward") that allows you to do: 由于我很难记住如何做到这一点,我创建了一个名为pf的小bash脚本(代表“port forward”),它允许你做:

$ pf 8080

This will forward the docker port 8080 to host port 8080 in the background (append -f to make it run in the foreground). 这会将docker端口8080转发到后台的主机端口8080(附加-f使其在前台运行)。 To use a different host port just do: 要使用其他主机端口,请执行以下操作:

$ pf 8090:8080

which maps the host port 8090 to 8080. 它将主机端口8090映射到8080。

To stop the port forwarding add -s : 要停止端口转发添加-s

$ pf 8090:8080 -s

(actually host port is enough as well: pf 8090 -s ). (实际上主机端口也足够了: pf 8090 -s )。 There are other options available as well so checkout the github page. 还有其他选项可用,请查看github页面。

如果您不想使用密码,我会补充说您应该只指向私钥。

ssh -L 8080:localhost:8080 -i ~/.docker/machine/machines/default/id_rsa docker@$(docker-machine ip default)

Just to enhance in script the answer of @VonC - currently if using Docker Toolbox on MacOS X, the default VM machine is "default". 只是为了增强脚本@VonC的答案 - 目前如果在MacOS X上使用Docker Toolbox,默认的VM机器是“默认”。 So a script to map all the exposed from container should look like: 因此,映射容器中所有暴露的脚本应如下所示:

for port in `docker port cassandra | cut -d'-' -f1`; 
do 
    port_num=`echo ${port} | cut -d'/' -f1`
    port_type=`echo ${port} | cut -d'/' -f2`
    echo "Create rule natpf1 for ${port_type} port ${port_num}"
    VBoxManage controlvm "default" natpf1 "${port_type}-port${port_num},${port_type},,${port_num},,${port_num}"
done

if you try to execute several times, a statement before creation should be added for deleting the existing rule: 如果您尝试多次执行,则应添加创建前的语句以删除现有规则:

VBoxManage controlvm "default" natpf1 delete "${port_type}-port${port_num}"

In the script it assumes that you have already port forward the ports from container to VM. 在脚本中,它假定您已经将端口从容器移植到VM。

docker port cassandra

gives output like: 给出如下输出:

7000/tcp -> 0.0.0.0:7000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM