Docker Networks – Best Way to Connect Two Separate Networks

dockernetworkingrouting

I am looking for the best method to connect two different Docker containers (ie allow them to ping eachother) that are on different networks.

I have a WireGuard container that's connected to wg-net in the 192.168.200.0/24 range, and another that's connected to the default bridge network, 172.17.0.0/24.

Pinging from one container to the other fails; and ideally I'd like to route certain IP traffic from one container through the other container, and thus connect to the WireGuard VPN.

Pinging either networks from the Docker host works fine.

What would be the best method to achieve this? I could create a 3rd network and connect the two containers to eachother, but that seems overkill. Is there no other way to have the Docker host route traffic between the two networks?

Best Answer

I could create a 3rd network and connect the two containers to eachother, but that seems overkill

"… Containers can join multiple networks which allows you to provide fine grained network policy for connectivity and isolation …"

Hence you can try solving this by introducing gateway CT that would be connected to both networks.

Otherwise you'd need to adjust netfilter's rules because Docker does enforce network access policy.