Linux
VERIFIED FIXES

Ubuntu 22.04: Docker containers intermittently lose DNS after host switches to systemd-resolved stub resolver during network changes

Environment

Environment

Developer laptop Ubuntu 22.04 with Docker Engine 20.10. Docker containers created with default bridge network. Host recently switched systemd-resolved to use the stub resolver (127.0.0.53) and NetworkManager manages DNS per connection (VPN and wired profiles). After connecting/disconnecting VPN or switching networks, containers fail to resolve hostnames until docker daemon restart.

Symptoms

Symptoms

Inside containers, DNS queries (ping, dig) return SERVFAIL or time out. Host DNS resolves fine. Restarting docker (.service) or recreating container fixes resolution temporarily. /etc/resolv.conf inside containers points to expected nameserver but the underlying 127.0.0.53 on host is not reachable from container namespace.

Observations

Observations

Host /etc/resolv.conf is a symlink to /run/systemd/resolve/stub-resolv.conf with nameserver 127.0.0.53. Container /etc/resolv.conf is populated at container start; when host network changes, systemd-resolved updates its listener or interface and docker-provided DNS path is no longer functional for existing containers. ip netns and nftables show no blocking. Docker's embedded DNS forwards to host resolver; that forwarder cannot reach the host stub when its socket changes.

Already attempted

Already attempted

Switched Docker daemon to use 8.8.8.8 in daemon.json (dns override) which fixes some containers but not desired for private DNS. Restarting docker fixes containers temporarily. Tried disabling stub resolver (use /run/systemd/resolve/resolv.conf) but switching breaks split-DNS for VPN.

Question

Question

How can I keep container DNS stable across host network changes and systemd-resolved updates without forcing containers to use public DNS? Is there a persistent safe configuration for Docker + systemd-resolved to preserve split-horizon and avoid docker restarts?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
hugohardware Answer
user
Posted 4w ago • 0 confirmations

The simplest reliable fix is to point Docker to a resolver that doesn’t move when the host’s network changes. Run a tiny forwarder on the host bound to a stable IP (e.g., 127.0.0.2) that forwards to systemd-resolved, then set that IP in /etc/docker/daemon.json under "dns" and restart Docker. Verify by switching networks/VPN and confirming container dig/ping still resolves. Rollback by restoring daemon.json and restarting Docker. Avoid relying on the 127.0.0.53 stub inside containers.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

systemd-resolved stub listener bound to a host namespace or socket that becomes unreachable from container network namespace after host network changes. 90%
Docker's embedded DNS proxy caches and continues forwarding to a resolver instance that was replaced on network change. 75%
VPN client replaces /run/systemd/resolve/resolv.conf and changes split-DNS records, causing mismatch between container DNS and host resolver view. 60%

Summary

Docker embeds a DNS proxy for containers that forwards to the host resolver configured when the container starts. systemd-resolved's stub resolver (127.0.0.53) is local to host network namespace and can change behavior or socket availability on network transitions (VPN connect/disconnect). Existing containers retain the stale DNS forwarding config and cannot reach the host stub after it moves, causing resolution failures. Workarounds include configuring Docker to forward to systemd-resolved's actual upstream files, using network namespaces, or ensuring a stable host-forwarder reachable by container networks.

Suggested Fixes

Diagnostic step 1
Configure Docker daemon to use systemd-resolved's upstream servers file instead of the stub: point Docker's dns to the IPs in /run/systemd/resolve/resolv.conf or set 'dns' in daemon.json to the internal resolver IP reachable from containers.
Diagnostic step 2
Alternatively, disable systemd-resolved stub and use the real resolver file (symlink /etc/resolv.conf to /run/systemd/resolve/resolv.conf) if split-DNS is not required, then restart docker.
Diagnostic step 3
As a more robust approach, run a small DNS forwarder on a stable host IP (127.0.0.1 or 127.0.0.2) reachable from containers and configure systemd-resolved to forward to it, or configure Docker to always use that forwarder.

Continue troubleshooting

Matched from the title, description and tags