【发布时间】:2018-01-12 13:23:08
【问题描述】:
探索 Docker 17.06。
我在 Centos 7 上安装了 docker 并创建了一个容器。使用默认网桥启动容器。我可以 ping 两个主机适配器,但不能 ping 外部世界,例如www.google.com
所有建议都是基于旧版本的 Docker 和它的 iptables 设置。
我想了解如何 ping 到外界,请问需要什么?
TIA!
【问题讨论】:
标签: docker networking iptables
探索 Docker 17.06。
我在 Centos 7 上安装了 docker 并创建了一个容器。使用默认网桥启动容器。我可以 ping 两个主机适配器,但不能 ping 外部世界,例如www.google.com
所有建议都是基于旧版本的 Docker 和它的 iptables 设置。
我想了解如何 ping 到外界,请问需要什么?
TIA!
【问题讨论】:
标签: docker networking iptables
我遇到了类似的问题,一个 api docker 容器需要连接到外部,但其他容器不需要。所以我的选择是将标志 --dns 8.8.8.8 添加到 docker run command ,这样容器就可以 ping 到外部。我认为这是一个容器的解决方案,如果您需要更多容器,也许其他响应会更好。 Here 文档。以及整行示例:
docker run -d --rm -p 8080:8080 --dns 8.8.8.8 <docker-image-name>
地点:
【讨论】:
在我的情况下,重新启动 docker daemon 有帮助
sudo systemctl restart docker
【讨论】:
如果 iptables 不是一个原因,并且如果您对更改容器网络模式没有任何限制 - 将其设置为“主机”模式。这应该可以解决这个问题。
【讨论】:
如果您能够从主机 ping www.google.com,请尝试执行以下步骤: 在主机上运行:
sudo ip addr show docker0
你会得到输出,其中包括:
inet 172.17.2.1/16 scope global docker0
docker 主机在 docker0 网络接口上的 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
希望它会有所帮助!
【讨论】:
请验证您现有的 iptables:
iptables --list
它应该向您显示包含源和目标详细信息的 iptables 列表。
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
如果源和目标都是 anywhere,它应该在 IP 之外 ping。(默认情况下它的 anywhere)
如果不使用这个命令来设置你的 iptable(DOCKER-USER)
iptables -I DOCKER-USER -i eth0 -s 0.0.0.0/0 -j ACCEPT
希望这会有所帮助!
【讨论】: