如何使我的网络上的其他计算机访问我的Docker容器(使用端口映射)? [英] How can I make other machines on my network access my Docker containers (using port mapping)?

查看:1393
本文介绍了如何使我的网络上的其他计算机访问我的Docker容器(使用端口映射)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OS X(VirtualBox驱动程序)上运行的最新的 Docker Toolbox RC

I have the latest Docker Toolbox RC running on OS X (VirtualBox driver).

Docker主机的IP是:

IP of Docker host is:

$ docker-machine ip
192.168.99.100

我正在运行这样的容器:

I'm running a container like this:

$ docker run -ti -p 4505:4505 my_image /bin/bash
[root@blah /]#

我尝试检查是否可以从我的网络上运行Windows 10的完全不同的机器访问此端口:

I've attempted to check whether I can access this port from an entirely different machine, running Windows 10, on my network:

$ nmap -p 4505 192.168.99.100

Starting Nmap 7.01 ( https://nmap.org ) at 2016-02-04 17:18 W. Europe Standard Time
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 0.74 seconds

如何使我的网络上的容器和其他机器看到对方?

How can I make the container and other machines on my network see each other?

编辑:下面接受的答案可以通过物理机器的IP地址访问指定端口上的容器。也就是说,您不能通过 192.168.99.100:4505 访问该容器。相反,它可以通过您的机器的< IP>:4505

the accepted answer below makes it possible to access the container on specified ports via the IP address of the physical machine. That is, you do not access the container via 192.168.99.100:4505. Instead, it can be accessible via your machine's <IP>:4505.

推荐答案

如果您使用Dockbox机器的VirtualBox驱动程序在Mac上运行Docker VM,那么您可以将端口从Mac转发到虚拟机,如下所示:

If you're using the VirtualBox driver with docker-machine to run a Docker VM on your Mac, then you can forward the port from your Mac to the VM like this:

VBoxManage controlvm myvirtualmachine natpf1 'myapp,tcp,,4505,,4505'

我们来分解一下:


  • VBoxManage 是可执行文件的名称控制VirtualBox。

  • controlvm 告诉VirtualBox你告诉一个虚拟机(而不是VM经理)做某事。

  • myvirtualmachine 是您的Docker VM的名称(您可以通过运行 docker-machine ls 找到它)。

  • natpf1 告诉虚拟机向前创建NAT端口。这是魔术开始发生的地方。

  • 'myapp,tcp ,, 4505,,4505' natpf1


    • myapp 是端口转发规则的名称。这是可选的。

    • tcp 是您要转发的流量类型。另一个选项是 udp

    • 此时可以使用主机IP,但可以将其留空。

    • 4505 是您要接收流量的Mac上的端口。这是您向网络上其他主机提供的端口号。

    • 此时还有一个访客IP的空间,但您可以将其留空。

    • 第二个 4505 是您要接收流量的Docker VM上的端口。您可以将其更改为任何您喜欢的,而不影响第一个端口。您只需要更改从Docker容器中显示的端口。

    • VBoxManage is the name of the executable that controls VirtualBox.
    • controlvm tells VirtualBox that you're telling a VM (rather than the VM manager) to do something.
    • myvirtualmachine is the name of your Docker VM (you can find this by running docker-machine ls).
    • natpf1 tells the VM to create a NAT port forward. This is where the magic starts to happen.
    • 'myapp,tcp,,4505,,4505' is a list of arguments to natpf1:
      • myapp is a name for the port forwarding rule. It's optional.
      • tcp is the type of traffic you want to forward. The other option is udp
      • There is room for a host IP at this point, but you can leave it blank.
      • 4505 is the port on your Mac where you want to receive the traffic. This is the port number you would give to the other hosts on your network.
      • There is room for a guest IP at this point, but you can leave it blank.
      • The second 4505 is the port on your Docker VM where you want to receive the traffic. You can change this to whatever you like without affecting the first port. You would just have to change the port you expose from your Docker container.

      您可以在 https://www.virtualbox.org上找到此命令的官方文档。 /manual/ch08.html#vboxmanage-controlvm

      这篇关于如何使我的网络上的其他计算机访问我的Docker容器(使用端口映射)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆