Fowarding traffic between containers in different networks

I’m experimenting with Docker networking and have set up the following scenario:

  1. Networks:
  • s1: User-defined Docker network 1
  • s2: User-defined Docker network 2
  1. Containers:
  • Container1 (connected to s1)
  • Container2 (connected to both s1 and s2)
  • Container3 (connected to s2)

The containers are just:

docker run -d --name c1 --network s1 alpine tail -f /dev/null

I’m trying to ping C3 from C1 and vice-versa, making C2 the middle man to foward this packet since its in both networks. But I can’t get this working. I have tried setting “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf and ran an entrypoint script with this iptables rules, without success:

iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -t nat -A POSTROUTING --out-interface eth0 -j MASQUERADE

Any tips? Am I going in the wrong direction?