DEV Community

Riccardo Caprai
Riccardo Caprai

Posted on

Docker Containers Unable to Access Internet? Check Your VPS Private Network

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Restart networking or apply Netplan:

sudo netplan apply
Enter fullscreen mode Exit fullscreen mode

After this, Docker containers will correctly use the public interface for internet access while still communicating on the private network.

Top comments (0)