0
0
DockerDebug / FixBeginner · 4 min read

Fix DNS Resolution Failed Error in Docker Containers

To fix 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.

bash
docker run busybox nslookup google.com
Output
;; connection timed out; no servers could be reached
🔧

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.

bash
docker run --dns 8.8.8.8 busybox nslookup google.com
Output
Server: 8.8.8.8 Address 1: 8.8.8.8 google-public-dns-a.google.com Name: google.com Address 1: 142.250.190.14
🛡️

Prevention

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.json with reliable DNS servers.
  • Restart Docker after changes.
  • Use network troubleshooting commands like docker network inspect to check network configs.
json
{
  "dns": ["8.8.8.8", "8.8.4.4"]
}
Output
Docker daemon will use Google DNS servers for all containers after restart.
⚠️

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.

Key Takeaways

Specify a reliable DNS server like 8.8.8.8 for Docker containers to fix DNS resolution errors.
Use the --dns flag or configure /etc/docker/daemon.json to set DNS servers globally.
Restart Docker daemon after DNS configuration changes to apply them.
Check container network settings and host DNS if DNS errors persist.
Use network troubleshooting commands to diagnose Docker network issues early.