Fix Docker Container DNS Resolution Failures
The Error: DNS Resolution Fails Inside Containers
Your containerized application cannot reach external services:
$ docker exec myapp curl https://api.example.com
curl: (6) Could not resolve host: api.example.com
Or with nslookup:
$ docker exec myapp nslookup google.com
;; connection timed out; no servers could be reached
The container has network connectivity but cannot translate hostnames to IP addresses.
Root Cause
Docker containers rely on DNS configuration injected at startup. Failures happen because:
- Host DNS uses
systemd-resolvedon 127.0.0.53 — this loopback address is unreachable from inside the container's network namespace - Docker's embedded DNS (127.0.0.11) is only available on user-defined networks, not the default
bridgenetwork - Corporate firewall or VPN blocks DNS traffic (port 53) from the Docker bridge interface
/etc/resolv.confinside the container points to an unreachable nameserver
Step-by-Step Fix
1. Confirm the problem is DNS, not network
# Test raw connectivity (use a known IP)
docker exec myapp ping -c 2 8.8.8.8
# If ping works but DNS doesn't, it's a DNS issue
docker exec myapp cat /etc/resolv.conf
2. Check what DNS server the container is using
docker exec myapp cat /etc/resolv.conf
If you see nameserver 127.0.0.53, that is the systemd-resolved stub listener and is not reachable from inside Docker.
3. Set an explicit DNS server for Docker
Edit /etc/docker/daemon.json:
{
"dns": ["8.8.8.8", "8.8.4.4"]
}
Restart Docker:
sudo systemctl restart docker
This forces all containers to use Google's public DNS instead of inheriting the host's broken config.
4. Or specify DNS per container
docker run --dns 8.8.8.8 --dns 8.8.4.4 myapp:latest
In Docker Compose:
services:
myapp:
image: myapp:latest
dns:
- 8.8.8.8
- 8.8.4.4
5. Use a user-defined network (recommended)
Docker's embedded DNS server (127.0.0.11) only works on user-defined networks and provides container name resolution:
# Create a custom network
docker network create mynet
# Run containers on it
docker run --network mynet --name myapp myapp:latest
docker run --network mynet --name mydb postgres:16
# Now myapp can resolve "mydb" by name
docker exec myapp ping mydb
6. Fix systemd-resolved on the host
Instead of bypassing systemd-resolved, make it work with Docker:
# Expose resolved on a real interface
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo tee /etc/systemd/resolved.conf.d/docker.conf <<EOF
[Resolve]
DNSStubListenerExtra=172.17.0.1
EOF
sudo systemctl restart systemd-resolved
Then set Docker to use the bridge gateway IP:
{
"dns": ["172.17.0.1"]
}
7. Check for iptables/firewall interference
# Verify Docker's iptables rules exist
sudo iptables -L DOCKER -n -v
# Check if port 53 traffic is being dropped
sudo iptables -L -n -v | grep 53
# If using UFW, allow Docker bridge traffic
sudo ufw allow in on docker0
Prevention Tips
- Always use user-defined networks instead of the default bridge — you get embedded DNS, container name resolution, and better isolation.
- Set DNS in
daemon.jsonas a baseline so all containers have a working fallback. - Include a DNS health check in your container:
healthcheck: test: ["CMD", "nslookup", "google.com", "||", "exit", "1"] interval: 30s retries: 3 - On VPN/corporate networks, use your internal DNS servers in
daemon.jsonrather than public DNS. - Test DNS in CI — add a
nslookupstep early in pipelines to catch issues before they cascade.
Was this article helpful?
DevSecOps Lead
Security-first mindset in everything I ship. From zero-trust architectures to supply chain security, I make sure your pipeline doesn't become your weakest link.
Related Articles
Docker "Permission Denied" On Mounted Volumes: Fix Unix Socket And File Ownership Errors
If you've spent more than five minutes with Docker volumes, you've seen it. That infuriating wall of red text: Or worse, the cryptic Unix socket variant: Y...
Fix Docker 'No Space Left on Device' Error
Reclaim disk space when Docker fills your drive — clean up dangling images, stopped containers, unused volumes, and build cache.
DNS Troubleshooting for DevOps: dig, nslookup, and Common Failures
A practical DNS troubleshooting guide for DevOps engineers — dig commands, nslookup patterns, common production failure modes, and how to diagnose each one.
Reducing Docker Image Build Times With BuildKit Cache Mounts
If your Docker builds are slow, cache mounts are probably the single highest-leverage fix available. I've seen Node.js image builds drop from 4 minutes to...
Docker Multi-Stage Builds: Shrink Images by 80% for Production
Cut Docker image sizes by up to 80% using multi-stage builds — practical patterns for Go, Node.js, and Python apps in production.
Docker CLI Cheat Sheet
Essential Docker CLI commands organized by task — build images, run containers, manage volumes and networks, compose services, and debug.
More in Docker
View all →Docker Multi-Stage Builds for Production-Ready Minimal Images
Shrink Docker images from 1.2GB to 45MB using multi-stage builds. Production Dockerfiles for Node.js, Go, and Python with real size comparisons.