Docker provides a built-in DNS server for resolving container names within a Docker network、This allows containers to communicate with each other using their service names (e.g., `web`, `db`) instead of IP addresses、Here's a structured explanation of how it works, common issues, and solutions.
1、How Docker DNS Works
Internal DNS Server:
Docker creates a local DNS server (127.0.0.11) inside each container、This server:
Resolves container names (e.g., `web`, `db`) within the same Docker network.
Forwards external DNS requests (e.g., `google.com`) to the host's DNS configuration.
Default DNS Configuration:
The container's `/etc/resolv.conf` file points to `127.0.0.11` (the internal DNS server) and inherits the host's DNS settings for external resolution、
Example:
bash
nameserver 127.0.0.11
options edns0
2、Common Issues & Fixes
A、Containers Can’t Resolve External Domains (e.g., `google.com`)
Cause: The host's DNS configuration is broken or not inherited by Docker、
Solutions:
1、Check Host DNS
Verify the host's `/etc/resolv.conf` (or Windows DNS settings) has valid nameservers、
bash
On Linux host:
cat /etc/resolv.conf
2、Set Custom DNS for Docker
Update the host's Docker daemon config (`/etc/docker/daemon.json`):
json
{
dns: [8.8.8.8, 8.8.4.4]
}
3、Override DNS for a Specific Container
Use `dns` in `docker run`:
bash
docker run dns=8.8.8.8 -it alpine nslookup google.com
B、Containers Can’t Resolve Each Other (e.g., `db` is unreachable)
Cause: Containers are on different networks or the DNS server isn’t configured correctly、
Solutions:
1、Ensure Same Docker Network
Connect containers to the same user-defined network:
bash
docker network create mynet
docker run network mynet name db -d mysql
docker run network mynet name web -d nginx
Now, `web` can resolve `db` via DNS.
2、Check Network DNS Configuration
In `docker-compose.yml`, add a `dns` section:
yaml
networks:
mynet:
dns:
8.8.8.8
3、Verify DNS Resolution Inside Container
Run a test container:
bash
docker run -it rm alpine nslookup db
If it fails, check if both containers are on the same network.
3、Advanced Configuration
A、Override DNS for All Containers
Docker Daemon Config (applies to all containers):
Edit `/etc/docker/daemon.json` and add:
json
{
dns: [8.8.8.8, 1.1.1.1]
}
B、Use a Custom DNS Server (e.g., for Internal Services)
Docker Compose:
yaml
services:
web:
dns:
10.0.0.10 Internal DNS server
Docker Run:
bash
docker run dns=10.0.0.10 -it alpine nslookup internal-service
C、Bypass Docker DNS (Rare Case)
If you need direct access to the host's DNS (e.g., for legacy apps), use `dns=127.0.0.1` (host's DNS) but only if the host has a valid DNS resolver.
Test DNS Resolution:
bash
docker exec -it <container_name nslookup 65.hk
Verify Network Links:
Ensure containers are on the same network:
bash
docker network inspect <network_name
Key Takeaways
| Scenario | Solution |
| External DNS fails | Fix host DNS or set `dns` in `daemon.json` |
| Internal container names fail | Ensure same Docker network + valid `dns` in compose |
| Need custom DNS for all containers| Configure `daemon.json` with `dns` array |
Docker’s DNS is designed to simplify service discovery within networks、Most issues stem from misconfigured host DNS or mismatched container networks、Always start by verifying the host’s DNS and network setup! 🐳