If your Docker containers can’t reach the internet, the issue might not be Docker itself. On VPS environments with a private network (like 192.168.x.x), the host’s default route may be misconfigured.
The VPS might have multiple interfaces: a private network (for internal communication) and a public network (for internet access). If the default route points to the private network, containers inherit this route and cannot reach external IPs.
Solution:
Check the host’s routing table:
ip route
If it looks something like this, it could be the problem:
default via X.X.X.X dev enp1s0 proto dhcp src X.X.X.X metric 100 
default via 192.168.0.X dev enp2s0 proto dhcp src 192.168.0.X metric 100 
Ensure the default route points to the public interface, not the private one.
If you are using Netplan on Ubuntu, increment the metric cost on the private interface by adding the following lines under that interface:
dhcp4-overrides:
    use-routes: false
Eg.
network:
    version: 2
    ethernets:
        enp1s0:
            dhcp4: true
            match:
                macaddress: aa:aa:aa:aa:aa:aa
            mtu: 8913
            set-name: enp1s0
        enp2s0:
            dhcp4: true
            dhcp4-overrides:
                route-metric: 200
            match:
                macaddress: bb:bb:bb:bb:bb:b
            mtu: 8913
            set-name: enp2s0
Restart networking or apply Netplan:
sudo netplan apply
After this, Docker containers will correctly use the public interface for internet access while still communicating on the private network.
 

 
    
Top comments (0)