如果你想使用固定IP,那么就接管Docker网络,启动容器使用--net=none然后使用官方文档里的方式直接进入namespace里创建网卡分配IP。
<pre>$ ip addr show docker0
21: docker0: ...
inet 172.17.42.1/16 scope global docker0
...
Create a pair of "peer" interfaces A and B,
bind the A end to the bridge, and bring it up</pre>
$ sudo ip link add A type veth peer name B
$ sudo brctl addif docker0 A
$ sudo ip link set A up
Place B inside the container's network namespace,
rename to eth0, and activate it with a free IP
$ sudo ip link set B netns $pid
$ sudo ip netns exec $pid ip link set dev B name eth0
$ sudo ip netns exec $pid ip link set eth0 address 12:34:56:78:9a:bc
$ sudo ip netns exec $pid ip link set eth0 up
$ sudo ip netns exec $pid ip addr add 172.17.42.99/16 dev eth0
$ sudo ip netns exec $pid ip route add default via 172.17.42.1
}}}
看一下官网文档就知道了 https://docs.docker.com/articles/networking/
3 个回复
xds2000 - 数人科技CTO
赞同来自: anthony0859 、[已注销]
https://yaxin-cn.github.io/Doc ... .html
许四两 - 北京数字安全公司运维工程师
赞同来自: jamlee
我没使用pipework,其实Docker分配IP这个逻辑很简单,只要你明白原理自己几行脚本就搞定了。
首先Docker创建一个网桥,然后通过网桥配置虚拟设备,成对创建,一个在宿主机上一个在namespace里,
你可以启动Docker daemon里指定你创建的网桥,然后分其它网段的IP。
如果你想使用固定IP,那么就接管Docker网络,启动容器使用
--net=none
然后使用官方文档里的方式直接进入namespace里创建网卡分配IP。<pre>$ ip addr show docker0
21: docker0: ...
inet 172.17.42.1/16 scope global docker0
...
Create a pair of "peer" interfaces A and B,
bind the A end to the bridge, and bring it up</pre>
$ sudo ip link add A type veth peer name B$ sudo brctl addif docker0 A
$ sudo ip link set A up
Place B inside the container's network namespace,
rename to eth0, and activate it with a free IP
$ sudo ip link set B netns $pid$ sudo ip netns exec $pid ip link set dev B name eth0
$ sudo ip netns exec $pid ip link set eth0 address 12:34:56:78:9a:bc
$ sudo ip netns exec $pid ip link set eth0 up
$ sudo ip netns exec $pid ip addr add 172.17.42.99/16 dev eth0
$ sudo ip netns exec $pid ip route add default via 172.17.42.1
}}}
看一下官网文档就知道了
https://docs.docker.com/articles/networking/
不过关于网络这块更强的功能已经加入了官方的further,后期版本可能就支持了。
我另一个女友叫五姑娘
赞同来自:
我使用pipe 或 weave 实现固定ip 并且跨物理机的