如何将特定IP分配给容器并使其在VM主机之外可访问? [英] How to assign specific IP to container and make that accessible outside of VM host?

查看:122
本文介绍了如何将特定IP分配给容器并使其在VM主机之外可访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将两个容器在VM主机之外的独立的IP地址( 192.168.0.222 192.168)上可用。 0.227 ),无端口映射。这意味着我希望通过使用其IP直接访问容器上的任何端口。我已经在VM主机之外的网络中运行机器,范围为192.168.0.1-192.168.0.221。

I wish to make two of my containers available outside of the VM host on their separate, specific IP addresses (192.168.0.222, 192.168.0.227), without port mapping. That means I wish to access any port directly on the containers by using its IP. I already have machines running in the network outside of the VM host in the range 192.168.0.1–192.168.0.221.

现在可以使用Docker 1.10.0和如果是,那么怎么样?

Is this now possible with Docker 1.10.0, and if so, how?

我在OS X 10.11上使用 docker版本1.10.0,build 590d5108 docker-machine版本0.6.0,使用boot2docker / VirtualBox驱动程序构建e27fb87

I'm on OS X 10.11 with docker version 1.10.0, build 590d5108 and docker-machine version 0.6.0, build e27fb87, using boot2docker/VirtualBox driver.

我一直在试图弄清楚一段时间,没有运气,我已经阅读了以下问题和答案:

I have been trying to figure this out for some while, without luck, and I've read the following questions and answers:

  • How to assign static public IP to docker container
  • How to expose docker container's ip and port to outside docker host without port mapping?
  • How can I make other machines on my network access my Docker containers (using port mapping)?

推荐答案

根据 Jessie Frazelle ,现在应该可以。

请参见所有的事情的IP


这太酷了,我几乎忍不住了。

This is so cool I can hardly stand it.

在Docker 1.10中,令人敬畏的libnetwork团队增加了为容器指定特定IP的能力。如果您想查看拉动请求,请访问: docker / docker#19001

In Docker 1.10, the awesome libnetwork team added the ability to specifiy a specific IP for a container. If you want to see the pull request it’s here: docker/docker#19001.



# create a new bridge network with your subnet and gateway for your ip block
$ docker network create --subnet 203.0.113.0/24 --gateway 203.0.113.254 iptastic

# run a nginx container with a specific ip in that block
$ docker run --rm -it --net iptastic --ip 203.0.113.2 nginx

# curl the ip from any other place (assuming this is a public ip block duh)
$ curl 203.0.113.2

# BOOM golden

这说明了 new docker run --ip 选项您现在可以在 docker network connect

That does illustrate the new docker run --ip option that you now see in docker network connect.


如果指定,重新启动停止的容器时,将重新应用容器的IP地址。如果IP地址不再可用,则容器无法启动。

If specified, the container's IP address(es) is reapplied when a stopped container is restarted. If the IP address is no longer available, the container fails to start.

确保IP地址可用的一种方式是在创建时指定 - ip-range 网络,并从该范围之外选择静态IP地址。这确保了当该容器不在网络上时,IP地址不会被给予另一容器。

One way to guarantee that the IP address is available is to specify an --ip-range when creating the network, and choose the static IP address(es) from outside that range. This ensures that the IP address is not given to another container while this container is not on the network.



$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network

$ docker network connect --ip 172.20.128.2 multi-host-network container2






可访问部分将像往常一样涉及端口转发

这篇关于如何将特定IP分配给容器并使其在VM主机之外可访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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