Resolve Docker DNS Issue on Server Restart
The Issue
I have been moving over to using Debian 13, and I strongly suspect that this issue is related to this, as I have not been experiencing this issue in the past. When I restart my virtual machines with docker containers running, they are suddenly unable to resolve hosts. They will start up and show as running correctly, which "hides" the problem, but you will end up getting wierd issues such as those services not being able to send emails because they can't resolve the SMTP host. Luckily this problem is easy enough to resolve and the following has been tested against Debian 13 where I experienced the issue.
Steps
Run the following set of commands to configure Docker to start up only after the network is ready and can find a nameserver before then restarting the service.
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/wait-for-dns.service > /dev/null <<'EOF'
[Unit]
Description=Wait for DNS configuration
After=network-online.target
Wants=network-online.target
Before=docker.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'until grep -qE "^nameserver[[:space:]]+[0-9a-fA-F:.]+$" /etc/resolv.conf; do sleep 1; done'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
sudo tee /etc/systemd/system/docker.service.d/network-online.conf > /dev/null <<'EOF'
[Unit]
Requires=wait-for-dns.service
After=wait-for-dns.service
EOF
# Enable it
sudo systemctl daemon-reload
sudo systemctl enable wait-for-dns.service
sudo systemctl restart wait-for-dns.service
# Restart docker service
sudo service docker restart
First published: 4th May 2026