Networking: user-defined bridge and Docker DNS
Create your own network and Docker gives you service discovery: containers find each other by name.
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
The embedded DNS server
Every container on a user-defined network is pointed at 127.0.0.11.
Why names beat IPs
Aliases and round-robin
What resolves, and what does not
| Lookup | Resolves on a user-defined network? |
|---|---|
container name (--name api) | yes |
--network-alias db | yes, many containers can share one |
| Compose service name | yes, Compose adds it as an alias |
| a container on a different network | no, no shared network |
| anything on the default bridge | no DNS there |
the image name (nginx:1.27) | never: it is not a host name |
Try it yourself
-
Step 1 · Create a network and two named containers
$ docker network create app $ docker run -d --name api --rm --network app alpine:3.20 sleep 300 $ docker run -d --name web --rm --network app alpine:3.20 sleep 300 -
Step 2 · Read the DNS server
On a user-defined network the container’s resolver is
127.0.0.11.$ docker exec web cat /etc/resolv.conf nameserver 127.0.0.11 -
Step 3 · Ping the name
$ docker exec web ping -c 1 api PING api (172.18.0.2): 56 data bytes 64 bytes from 172.18.0.2: seq=0 ttl=64 -
Step 4 · Recreate api and ping the name again
The IP can change. The name still resolves.
$ docker stop api $ docker run -d --name api --rm --network app alpine:3.20 sleep 300 $ docker exec web ping -c 1 api PING api (172.18.0.3): 56 data bytes $ docker stop api web $ docker network rm app
Recap
- A user-defined bridge comes with DNS at
127.0.0.11; the default bridge does not. - Containers resolve each other by container name or network alias.
- Unknown names are forwarded to the host resolver, so the internet still works.
- Resolution is scoped to shared networks.
Quick check
Why does
ping api keep working after you recreate the api container with a new IP?Show answer
The name is looked up again through Docker DNS, which now returns the new address. Nothing in the caller stored the old IP.