DNS inside Docker: how containers find each other
On user-defined networks Docker runs an embedded DNS server at 127.0.0.11 that resolves container names, service names and aliases to container IP addresses.
- Updated Nov 27, 2026
- Markdown
Explain how containers resolve each other by name, then debug a failed lookup from inside a container.
- Docker Engine and Compose on a Linux server
- A stack with two containers on one network
Containers on a user-defined network find each other by name because Docker runs an embedded DNS server for them, reachable at 127.0.0.11 inside each container. It resolves container names, Compose service names and network aliases to IP addresses. The default bridge network does not offer this, which is why names fail on it.
How it works#
When a container joins a user-defined network, Docker writes its /etc/resolv.conf to point at 127.0.0.11. That address is not a real server on the network. Docker intercepts queries sent there inside the container’s own namespace and answers them from its records of the network. If the name matches a container, service or alias on a network the container belongs to, the answer is that container’s IP on that network. If not, Docker forwards the query to the upstream resolvers, which come from the host’s DNS settings unless you override them.
This design means container IPs can change on restart without breaking anything: the name stays stable and Docker updates the answer. Never hard-code a container IP. For the underlying idea of DNS itself, see DNS.
Names, services and aliases#
On a user-defined network, a container is resolvable by:
- its container name (
--name db), - its hostname, which defaults to the short container ID,
- any network alias you give it with
--network-alias.
In Compose, each service gets its service name as a DNS name on the project network, and you can add extras:
services:
web:
image: nginx
depends_on:
- db
db:
image: postgres:16
networks:
default:
aliases:
- databaseHere web can use db or database as the hostname. Several containers sharing one alias gives simple round-robin answers, which is a rough way to spread connections but not a real load balancer. A container only resolves names of peers on networks it shares, so putting a service on two networks lets it bridge them. See Docker Compose basics for how Compose builds the default network.
The default bridge has no name resolution#
On the default bridge network, containers can talk by IP but not by name. This catches people who run two docker run commands without --network and then try ping db. The fix is to create a user-defined network and attach both containers:
docker network create appnet
docker run -d --name db --network appnet postgres:16
docker run -d --name web --network appnet nginxThe old --link flag provided names on the default bridge, but it is legacy and a user-defined network is the right answer. The reasons the two bridges differ are covered in Docker bridge vs host networking.
Looking at resolv.conf#
Inside a container on a user-defined network:
docker exec web cat /etc/resolv.confYou should see a nameserver 127.0.0.11 line and some options such as ndots:0. On the default bridge, resolv.conf instead copies the host’s nameservers. If you run in host mode, the container uses the host’s resolv.conf directly. Exact contents vary by Docker version and host setup, so read your own output instead of expecting a fixed text.
Custom DNS servers#
To change where non-container names are looked up, set DNS servers at one of three levels:
docker run --dns 192.0.2.53 --dns-search example.com alpineIn Compose, use dns: and dns_search: on a service. For every container on the host, put a dns array in /etc/docker/daemon.json and restart the daemon. Be aware that restarting Docker briefly stops containers unless live restore is configured, so schedule it. Custom DNS affects the forwarded lookups. Container names still resolve through the embedded server.
Check it worked#
Resolve a peer from inside the client container with the same path an app uses:
docker exec web getent hosts dbA working lookup prints an IP address followed by the name. If the image has nslookup or dig, you can also run docker exec web nslookup db 127.0.0.11. Many slim images do not include those tools, but getent comes with glibc-based images. On Alpine, getent may be missing, and ping -c1 db or nslookup db from BusyBox can stand in. For running commands this way, read run commands inside a container.
Next confirm the network membership:
docker network inspect appnet --format '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'Both containers should be listed. If one is missing, it is on another network.
Edge cases and gotchas#
- Different networks: Two Compose projects have separate networks, so their services do not see each other by name unless you share an external network.
- Stale caches: Applications that resolve a name once and cache it, such as some connection pools, keep the old IP after the target container is recreated. Restart the client or set a short cache time.
- Name vs host port: Between containers you use the container port, such as
db:5432, not the host port you published. - Search domains: Short names can pick up unexpected suffixes from the host’s search domain, so a typo may resolve to something real. Use the exact service name.
- Underscores in names: Hostnames with underscores can fail strict resolvers in some software. Prefer hyphens in service names.
- Start order: A container that starts before its peer exists may see a lookup failure at boot. Use retries or a health check with
depends_onconditions.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| Name does not resolve between two containers | Default bridge, or different networks | Attach both to one user-defined network |
| Resolves, but connection refused | Wrong port, or app listens on 127.0.0.1 | Use the container port and bind the app to 0.0.0.0 |
| Resolves to an old IP | Client cached the lookup | Restart the client or lower its DNS cache time |
| External names fail, container names work | Upstream DNS unreachable | Set —dns or dns: to a working resolver |
| getent not found in container | Minimal image | Use nslookup or ping, or debug from a helper container on the same network |
Quick reference#
docker network create appnet
docker run -d --name db --network appnet postgres:16
docker exec web getent hosts db
docker exec web cat /etc/resolv.conf
docker run --dns 192.0.2.53 alpine nslookup example.com
docker network inspect appnetRelated lessons#
- Docker networks explained for the network types behind this behavior.
- Docker bridge vs host networking to see which modes include the embedded resolver.
- Docker Compose basics for the project network that makes service names work.
- Run commands inside a container for using docker exec to debug lookups.
- Check DNS with dig for the same tooling against public DNS.
Next#
Continue with bind mounts vs volumes: permissions and ownership.
Check what you learned
Frequently asked questions
How do Docker containers resolve each other by name?
Containers on a user-defined network query an embedded DNS server at 127.0.0.11. It answers with the IP of the container whose name, service name or network alias matches.
Why can't my container resolve another container's name?
Most often they are on different networks, or both are on the default bridge. Put them on the same user-defined network, or use a Compose project where this is automatic.
How do I set a custom DNS server for a Docker container?
Use docker run --dns 192.0.2.53, or dns: in Compose, or a dns key in /etc/docker/daemon.json for all containers. The setting applies to the external lookups that the embedded server forwards.
What is the 127.0.0.11 nameserver in resolv.conf?
It is Docker's embedded DNS resolver. It lives in the container's network namespace and handles container names first, then forwards other lookups to the configured upstream servers.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.