【问题标题】:Docker containers in the same network cannot communicate with each other同一网络中的 Docker 容器无法相互通信
【发布时间】:2020-01-16 22:43:04
【问题描述】:

我是第一次在 Centos 中使用 Docker。

在部署两个容器时,我发现我在互联网上遇到路由问题,然后我发现我什至无法让它们相互通信(尽管在默认的bridge 网络上)。

在一个容器中发生这种情况:

/ # ip a | grep 172
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
/ # ping 172.17.0.3
PING 172.17.0.3 (172.2.0.3): 56 data bytes
^C
--- 172.17.0.3 ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss

另一方面,同样的行为:

/ # ip a | grep 172
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
/ # ping 172.17.0.2
PING 172.17.0.2 (172.2.0.2): 56 data bytes
^C
--- 172.2.0.2 ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss

并且他们在同一个网络中:

$ docker inspect 91767dd3adfa | grep -i networkid
                    "NetworkID": "d36d28507f9cc3f6c40437330af3778c117d303e106de0b3b43ad7919d2791c7",
$ docker inspect a393490d8d02 | grep -i networkid
                    "NetworkID": "d36d28507f9cc3f6c40437330af3778c117d303e106de0b3b43ad7919d2791c7",
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
d36d28507f9c        bridge              bridge              local
f32f4c8d6187        host                host                local
5693790b1713        none                null                local

为什么会这样?我在 Ubuntu 和 MacOS 中使用过 Docker,它可以无缝运行。

【问题讨论】:

  • 请确保您的网络中没有其他 172.17.0.2/16 vpn。
  • 我不知道,@gile。
  • 您确定这不仅仅是 ICMP 问题吗?您是否尝试过诸如 SSH 或 http 之类的高级连接?
  • 我试过 curl 和 wget。
  • 您使用的是陈旧的传统桥接网络,它有一些特点。您应该docker network create 自定义网络(默认选项很好),docker run --net 该网络上的两个容器,它们将能够使用彼此的--name 作为主机名。完成此操作后,永远不要查找容器的私有 IP 地址。

标签: linux docker centos docker-network


【解决方案1】:

我在这里看到的是完全是你的错误,机器的IP在网络172.17.0.0/16 但是您正尝试在 172.2.0.0/16 上 ping 机器,因此它将无法工作,因为该网络的机器超出了范围,而且现有机器的 IP 不是您要发送 ping 请求的机器。

【讨论】:

  • 实际上我写错了问题(命令及其结果已被编辑)。
【解决方案2】:

我找到了解决办法。

启用防火墙以允许进出docker0 网络的连接。

这是使用以下命令执行的:

iptables -I INPUT -s <network> -i docker0 -m comment --comment "00015 input on docker0" -j ACCEPT
# accept any package coming from the network to docker0 interface
iptables -I FORWARD -m comment --comment "00010 conntrack on forward" -m state --state RELATED,ESTABLISHED -j ACCEPT
# maintain any 'session' or link to be able to return packages fro meth0 to docker0 (answer). Very tightened to the existance of a 'nat', otherwise this entry does not have any impact
iptables -I FORWARD -s <network> -i docker0 -o eth0 -m comment --comment "00011 forward to eth0 from docker0" -j ACCEPT
#forward packages
iptables -t nat -I POSTROUTING -s  <network> -o eth0 -m comment --comment "00013 masquerade on eth0 from docker0"
-j MASQUERADE
# create nat in order for any package that goes out of the host to be able to come back using the ip of the host and after the ip of the container

【讨论】:

    【解决方案3】:

    尝试创建一个新网络并将容器关联到这个网络,网络默认 docker "bridge" 不能像其他手动创建的网络那样工作

    【讨论】:

    • 您是指一般情况下还是在 Centos 中?如果您的意思是一般情况,那么情况并非如此,因为在 ubuntu 和 macos 中它可以工作。
    • 不兄弟,docker中的网络,你可以创建其他网络。我创建了新的网络network-test-docker,它是网络我将容器连接到网络中的容器设置ip静态或主机名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2017-01-28
    • 2022-11-10
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多