Fix DNS Resolution Failed Error in Docker Containers
DNS resolution failed errors in Docker, update the container's DNS settings by specifying a reliable DNS server like 8.8.8.8 in the Docker daemon or container run command. This ensures containers can resolve domain names correctly and connect to the internet.Why This Happens
Docker containers rely on DNS to convert domain names into IP addresses. If Docker's default DNS settings are missing or incorrect, containers cannot resolve domain names, causing the DNS resolution failed error.
This often happens when the host's DNS settings are unusual or Docker's internal DNS server is unreachable.
docker run busybox nslookup google.com
The Fix
Specify a working DNS server explicitly for Docker containers. You can do this by adding the --dns flag when running a container or by configuring the Docker daemon to use a public DNS like Google DNS (8.8.8.8).
This change ensures containers use a reliable DNS server and can resolve domain names properly.
docker run --dns 8.8.8.8 busybox nslookup google.comPrevention
To avoid DNS resolution errors in the future, always verify your Docker daemon's DNS settings especially if your host uses custom or VPN DNS servers.
- Configure
/etc/docker/daemon.jsonwith reliable DNS servers. - Restart Docker after changes.
- Use network troubleshooting commands like
docker network inspectto check network configs.
{
"dns": ["8.8.8.8", "8.8.4.4"]
}Related Errors
Other common network errors in Docker include:
- Network unreachable: Caused by incorrect network mode or firewall blocking.
- Connection refused: Service inside container not running or port not exposed.
- Timeout errors: Due to slow network or DNS delays.
Fixes usually involve checking container network settings, firewall rules, and service status.