大网数据广告活动
服务器运维问答

Routing among different docker networks on the same host machine

· 52CE 我爱测

I have created two docker networks. docker network create --subnet=172.18.0.0/16 Docker_network_1 docker network create --subnet=172.19.0.0/16 Docker_network_2 On each one of them I run two different containers: docker run --rm -…

问题原文

原帖链接 · gaetano · CC BY-SA 3.0

I have created two docker networks.

docker network create --subnet=172.18.0.0/16 Docker_network_1
docker network create --subnet=172.19.0.0/16 Docker_network_2

On each one of them I run two different containers:

docker run --rm -it --name Container_1 --net Docker_network_1  alpine /bin/sh
docker run --rm -it --name Container_2 --net Docker_network_2  alpine /bin/sh

Container_1 has IP 172.18.0.2 whereas Container_2 has IP 172.19.0.2.

From Container_1 I can ping the docker interface IP 172.19.0.1 which belongs to Docker_network_2 but I cannot ping Container_2 IP 172.19.0.2.

I don't understand why since "routing" on my host machine seems correct:

#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    1024   0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-ea28cf2d7108
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-244606ad6705

社区高分解答

原帖链接 · montjoy · CC BY-SA 4.0

Tailing on @user 's answer. A little bit safer way to do is is to add rules to allow the networks to talk to each other instead of flushing everything.

This is what worked for me:

sudo iptables -I DOCKER-ISOLATION-STAGE-2 -o docker0 -i othernet -j ACCEPT
sudo iptables -I DOCKER-ISOLATION-STAGE-2 -o othernet -i docker0 -j ACCEPT

I have yet to find a non-hacky way to do this automatically.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。