Docker NetworkingLesson 3.4
Docker DNS and service discovery between containers
embedded DNS server, container name resolution, network alias, --network-alias, DNS round-robin, service discovery pattern
How Containers Find Each Other by Name
When containers run on a custom bridge network, Docker runs an embedded DNS server at 127.0.0.11 inside each container. Any query for a container name is resolved to that container's IP automatically.
Name Resolution in Practice
docker network create backend
docker run -d --name redis --network backend redis:7
docker run -it --network backend alpine sh
# Inside the alpine container:
nslookup redis # Resolves to redis container IP
wget -qO- http://redis:6379 # Can connect by name
Network Aliases
Aliases let multiple containers share one DNS name — useful for horizontal scaling or blue-green deploys:
docker run -d \
--name api-v1 \
--network backend \
--network-alias api \
my-api:1.0
docker run -d \
--name api-v2 \
--network backend \
--network-alias api \
my-api:2.0
Both containers answer to the hostname api. Docker performs DNS round-robin between them, distributing requests across both instances. This is the basis of Docker Compose's built-in service scaling.
