每次当我启动一个容器与docker,它获得了不同的IP

如何修复一个容器的静态IP?

首先,我启动一个容器并检查它,它说

"NetworkSettings": { "IPAddress": "XX.XX.206.98", "IPPrefixLen": 27, "Gateway": "XX.XX.206.105", "Bridge": "public", "PortMapping": null, "Ports": {} }, 

然后我停下来,重新启动,就像

 "NetworkSettings": { "IPAddress": "XX.XX.206.99", "IPPrefixLen": 27, "Gateway": "XX.XX.206.105", "Bridge": "public", "PortMapping": null, "Ports": {} }, 

正如你所看到的,它改变了。 我刚创build了一个名为public的网桥,并启动了docker,并添加了-b=public 。 我如何设置一个容器的静态IP?

从DOCKER 1.10开始

 # 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 

UPDATE

现在获得静态IP的唯一方法是通过两个脚本: pipework或ovs-docker

使用Open vSwitch作为多托pipedocker集装箱的“电池供电”版本有一个很强的方向。

留意sockets板 。


此行为是devise使然。

有一个非常有趣的讨论 ,在未来的版本中改变它。

到目前为止,你可以做到这一点的唯一方法是回落到Linux容器:

 docker run \ -n=false \ -lxc-conf="lxc.network.type = veth" \ -lxc-conf="lxc.network.ipv4 = 172.16.42.20/24" \ -lxc-conf="lxc.network.ipv4.gateway = 172.16.42.1" \ -lxc-conf="lxc.network.link = docker0" \ -lxc-conf="lxc.network.name = eth0" \ -lxc-conf="lxc.network.flags = up" \ -i -t my_image:my_tag /bin/bash 

因此, -n=false会禁用自动泊坞网连接,所有-lxc-conf选项都会根据您的需要实际定义虚拟networking。