使不通虚拟机中的docker通信
web1的ip是192.168.8.126
web2的ip是192.168.8.127
mysql的ip是192.168.8.130
(1)修改docker网桥ip
web1:
ifconfig docker0 172.17.1.1 netmask 255.255.255.0
service docker restart
web2:
ifconfig docker0 172.17.2.1 netmask 255.255.255.0
service docker restart
Mysql:
ifconfig docker0 172.17.3.1 netmask 255.255.255.0
service docker restart
(2)添加路由
#web1
route add -net 172.17.3.0 netmask 255.255.255.0 gw 192.168.8.130
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -s 172.17.1.0/24 ! -d 172.17.0.0/16 -j MASQUERAD
#web2:
route add -net 172.17.3.0 netmask 255.255.255.0 gw 192.168.8.130
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -s 172.17.2.0/24 ! -d 172.17.0.0/16 -j MASQUERADE
#mysql:
route add -net 172.17.1.0 netmask 255.255.255.0 gw 192.168.8.126
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -s 172.17.3.0/24 ! -d 172.17.0.0/16 -j MASQUERADE
route add -net 172.17.2.0 netmask 255.255.255.0 gw 192.168.8.127
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -s 172.17.3.0/24 ! -d 172.17.0.0/16 -j MASQUERADE
(3) 测试
在mysql主机上的容器里ping web1 和 web2:
ping 192.168.8.126
ping 192.168.8.127