环境 | 网段 |
---|---|
192.168.0.202(m2) | 172.17.0.0/16 |
192.168.0.203 (m3) | 172.16.0.0/16 |
[m3]# docker network create mynet --subnet 172.16.0.0/16
[m3] # docker run -itd --net mynet --ip 172.16.0.2 busybox # 指定为刚才新创建的网络mynet
[m2]# route add -net 172.16.0.0 netmask 255.255.255.0 gw 192.168.0.203
[m2] # docker run -itd --ip 172.17.0.2 --privileged=true busybox # 需要用特权模式进入
[m2]# route add -host 192.168.0.203 gw 172.17.0.1
[m2] # iptables -t nat -A POSTROUTING -s 172.17.0.2 -j SNAT --to 192.168.0.202
现在本机docker能够ping通对端宿主机
[m3]# docker network create mynet2 --subnet 172.18.0.0/16
[m3] # docker run -itd --net mynet2 --ip 172.18.0.2 -h docker3 busybox # 主机名docker3
echo '172.18.0.2 docker3' >> /etc/hosts
route add -host docker3 gw 192.168.0.203