Ubuntu 22.04: Docker containers intermittently lose DNS after host switches to systemd-resolved stub resolver during network changes
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
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
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
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
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?
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Linux55% match
Ubuntu 22.04 laptop: Docker containers lose DNS after suspend/resume due to systemd-resolved stub resolver race with NetworkManager
Open1 answer - Linux52% match
Docker containers lose DNS after host switched to systemd-resolved stub resolver on Ubuntu 22.04
Solved1 answer - Linux49% match
Ubuntu 22.04 laptop: Docker containers lose DNS resolution after suspend/resume (systemd-resolved stub resolver conflict)
Open0 answers - Virtualization48% match
Docker containers lose DNS resolution after laptop resume on Ubuntu 22.04 due to systemd-resolved stub resolver change
Open0 answers - Linux35% match
Docker containers lose DNS resolution after laptop suspend/resume on Ubuntu 22.04
Open0 answers