My goal is to run two Docker containers on separate networks and have my host (Ubuntu 22.04) perform NAT so that the first network can reach the second.
My setup:
docker network create network1
docker network create network2
docker run --rm -it --network network1 ubuntu:22.04 bash
# In other terminal
docker run --rm -it --network network2 ubuntu:22.04 bash
The first container has an address of 172.19.0.2 and the second has an address of 172.20.0.2.
Running ip addr on my host, I see that the interfaces are br-deadbeef and br-feedbeef, respectively.
I then run
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A FORWARD -i br-deadbeef -o br-feedbeef -j ACCEPT
iptables -A FORWARD -i br-feedbeef -o br-deadbeef -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A POSTROUTING -o br-feedbeef -j MASQUERADE
on the host as root.
However, ping 172.20.0.2 from the first container doesn't succeed. Running Wireshark on the host shows the ICMP packet on the br-deadbeef network going from 172.19.0.2 to 172.20.0.2 but there's no reply.
What am I missing?