Sub topic 6
Reaching containers by name with Docker DNS
This chapter assumes you can run containers with docker run, read basic TCP/IP terms (IP address, subnet, port, DNS), and have created a user-defined bridge network as described in the previous chapter.
Why names beat IP addresses
Docker assigns each container an IP address from the network's subnet when the container starts. That address can change after a restart or when you recreate the container. Hard-coding it in another container's configuration breaks easily.
Docker solves this with an embedded DNS server. On user-defined networks, a container can reach another container by its name, and Docker resolves the name to the current IP address.
How the embedded DNS server works
Each container on a user-defined network gets a /etc/resolv.conf that points to 127.0.0.11. This address is the embedded DNS server, which Docker runs inside the container's network namespace and backs with the daemon's data.
When a container looks up a name, the server follows these steps:
- It checks whether the name matches a container, or a network alias, on a network the asking container is attached to.
- If it matches, it returns that container's IP address on the shared network.
- If it does not match, it forwards the query to the DNS servers configured on the host, so external names such as
example.comstill resolve.
Try it
The following commands create a network, start a web server, and look up its name from a second container.
docker network create app-net
docker run -d --name web --network app-net nginx:alpine
docker run --rm --network app-net curlimages/curl http://web
docker run --rm --network app-net busybox nslookup webThe curl command returns the nginx welcome page. The nslookup output shows the server 127.0.0.11 and the address of web.
Network aliases
A container can have more names than its container name. The --network-alias option adds a name on a specific network.
docker run -d --name api-1 --network app-net --network-alias api nginx:alpine
docker run --rm --network app-net busybox nslookup apiIf several containers share the same alias on one network, Docker returns all of their addresses and rotates the order. This gives simple DNS round-robin load balancing. It is not health-aware, and clients that cache a result keep using one address.
What does not resolve
Name resolution depends on the network, not only on the container.
| Situation | Names resolve? | Reason |
|---|---|---|
| Two containers on the same user-defined network | Yes | Both are known to the embedded DNS server |
| Two containers on different user-defined networks | No | A container only resolves names on networks it is attached to |
| Containers on the default `bridge` network | No | The default bridge has no automatic name resolution |
| Host shell to a container name | No | The host does not use Docker's embedded DNS |
A container attached to several networks can resolve names on each of them. This is how a proxy reaches services on two separate networks.
Checking and fixing resolution
If a name fails to resolve, work through these checks:
- Confirm both containers are on the same network with
docker network inspect app-net. - Check the spelling of the name, and whether you used the container name or an alias.
- Run
nslookupfrom inside the client container, not from the host. - Look for a container that was started on the default bridge by mistake.
You can connect a running container to a network without recreating it:
docker network connect app-net some-containerExercise
A container named client is on network net-a. A container named db is on network net-b. The command ping db from client fails with "bad address". What is the cause, and how do you fix it without changing either container's image?
Show answer
The embedded DNS server only resolves names on networks the asking container is attached to. client and db share no network, so db is unknown to client. Attach one container to the other's network, for example docker network connect net-b client. Then ping db resolves.
Exercise
You start two containers on the default bridge network and run nslookup for one from the other. It fails. Which command creates a setup where it works?
Show answer
Create a user-defined network with docker network create app-net. Start both containers with --network app-net. Names then resolve through the embedded DNS server at 127.0.0.11.