Docker容器不能ping通外部世界 – iptables
探索docker17.06。
我已经在Centos 7上安装了docker,并创build了一个容器。 用默认网桥启动容器。 我可以ping通两个主机适配器,但不是外部世界,例如www.google.com
所有的build议是基于旧版本的Docker,它是iptables的设置。
我想了解如何ping到外面的世界,需要什么?
TIA!
如果您能够从主机ping通www.google.com,请按以下步骤操作:在主机上运行:
sudo ip addr show docker0
你将得到的输出包括:
inet 172.17.2.1/16 scope global docker0
docker主机在docker0networking接口上的IP地址为172.17.2.1。
然后启动容器:
docker run --rm -it ubuntu:trusty bash
并运行
ip addr show eth0
输出将包括:
inet 172.17.1.29/16 scope global eth0
您的容器的IP地址为172.17.1.29。 现在看看路由表:运行:
route
输出将包括:
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 172.17.2.1 0.0.0.0 UG 0 0 0 eth0
这意味着docker主机172.17.2.1的IP地址被设置为默认路由,并且可以从您的容器访问。
现在尝试ping你的主机ip:
root@e21b5c211a0c:/# ping 172.17.2.1 PING 172.17.2.1 (172.17.2.1) 56(84) bytes of data. 64 bytes from 172.17.2.1: icmp_seq=1 ttl=64 time=0.071 ms 64 bytes from 172.17.2.1: icmp_seq=2 ttl=64 time=0.211 ms 64 bytes from 172.17.2.1: icmp_seq=3 ttl=64 time=0.166 ms
如果这很有效,你可以ping通www.google.com
希望它会帮助!
请validation您现有的iptables:
iptables --list
它应该显示你的iptables列表的源和目标的详细信息。
target prot opt source destination DOCKER-USER all -- anywhere anywhere
如果源和目的地都在任何地方 ,它应该ping外部IP(默认情况下,它的任何地方 )
如果不使用这个命令来设置你的iptable(DOCKER-USER)
iptables -I DOCKER-USER -i eth0 -s 0.0.0.0/0 -j ACCEPT
希望这会有所帮助!
一个好主意是在启动容器时使用:
docker run -p <host_port>:<docker_port>
这将确保您的桥接networking将绑定docker和主机在端口,然后您可以从您的容器访问外部世界。
如果你不想使用-p,你可以检查你的iptables,容器,所以放在这里?
参考:
https://docs.docker.com/v1.11/engine/reference/commandline/run/