Docker – Routing among different docker networks on the same host machine

dockernetworking

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

Best Answer

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.