如何修复容器的静态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",
如你所见,它改变了.我刚创建了一个名为public的桥,并在-b = public的情况下启动了docker.如何为容器设置静态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作为多个托管docker容器的“电池包含”版本有一个强烈的方向.
留意socketplane.
此行为是设计使然.
在将来的版本中有一个very interesting discussion用于更改它.
到目前为止,唯一可行的方法是回退到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会禁用自动docker网络,所有-lxc-conf选项都会根据您的需要实际定义虚拟网络.