Linux
VERIFIED FIXES

Docker containers lose DNS after host switched to systemd-resolved stub resolver on Ubuntu 22.04

Environment

Environment

Ubuntu 22.04 server running Docker Engine 20.10 on APT-updated host; containers use default bridge network and rely on Docker-generated /etc/resolv.conf inside container.

Symptoms

Symptoms

Containers intermittently cannot resolve external hostnames after switching the host to systemd-resolved's DNSStubListener=udp (i.e., /etc/resolv.conf points to 127.0.0.53). Host name resolution works normally. Restarting Docker or recreating containers temporarily fixes DNS until resolved is restarted.

Observations

Observations

Inspecting a container shows /etc/resolv.conf contains 127.0.0.53 copied from host. From inside containers, queries to 127.0.0.53 fail. Creating containers with explicit DNS in daemon.json (e.g. 8.8.8.8) fixes the containers. Docker daemon.json is empty; NetworkManager is managing network configuration.

Already attempted

Already attempted

Restarted systemd-resolved and Docker in various orders; created /etc/docker/daemon.json with "dns": ["8.8.4.4","8.8.8.8"] then restarted Docker which resolved the issue for newly created containers; did not change host-wide resolver behavior.

Question

Question

How should Docker be configured on a system using systemd-resolved so containers keep working after resolver restarts and the host uses the stub resolver at 127.0.0.53?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
ellielectron Answer
user
Posted 1mo ago • 0 confirmations

Docker inherits the host's resolv.conf at container creation; if that file points to the systemd stub (127.0.0.53) containers can't reach it. Fast fixes: (A) Configure Docker to use known public DNS servers by creating /etc/docker/daemon.json with {"dns":["1.1.1.1","8.8.8.8"]} and restart dockerd, or (B) switch systemd-resolved to publish real upstream servers by setting DNSStubListener=no in /etc/systemd/resolved.conf, restart systemd-resolved, then symlink /etc/resolv.conf to /run/systemd/resolve/resolv.conf. Verify with docker run --rm busybox nslookup google.com. Both changes are reversible: remove the daemon.json dns entry or set DNSStubListener=yes and restore the original /etc/resolv.conf symlink.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Docker copied host /etc/resolv.conf containing 127.0.0.53 (systemd stub) which is unreachable from inside containers 90%
systemd-resolved restarting changes /etc/resolv.conf and Docker doesn't update containers until recreating them 60%
NetworkManager or other network manager is toggling resolver configuration on interface reconfiguration 30%

Summary

Docker copies the host's /etc/resolv.conf at container creation; when that file points at the host's stub (127.0.0.53) the container cannot reach it because 127.0.0.53 is the loopback address inside the container, not the host. This misconfiguration is the most likely cause. Alternate approaches are to make containers use fixed external DNS servers via daemon.json, or change the host so systemd-resolved exposes the upstream servers in a file Docker can read (for example symlink /etc/resolv.conf to /run/systemd/resolve/resolv.conf) or disable the stub listener and allow DNS on the normal interfaces.

Suggested Fixes

Diagnostic step 1
Short-term: set explicit DNS in /etc/docker/daemon.json and restart Docker so containers use stable resolvers (example: { "dns": ["1.1.1.1","8.8.8.8"] }).
Diagnostic step 2
Preferred: disable systemd-resolved stub listener and point /etc/resolv.conf at /run/systemd/resolve/resolv.conf (edit /etc/systemd/resolved.conf, set DNSStubListener=no, restart resolved, then ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf).
Diagnostic step 3
Alternative: configure systemd-resolved to publish upstream servers to /etc/resolv.conf (via the symlink) so Docker copies actual upstream IPs.

Continue troubleshooting

Matched from the title, description and tags