Skip to main content

Understanding Docker DNS and Service Discovery

Learn how Docker's embedded DNS lets containers find each other by name on user-defined networks and Compose, and how it maps to Kubernetes DNS.

44 Terms

Overview and What You Will Learn

Docker's embedded DNS server lets containers on the same user-defined network reach each other by container name, service name or network alias, instead of by IP address. It listens at 127.0.0.11 inside every container on a user-defined network and always returns the container's current IP, so restarts do not break connections. The default bridge network does not have this feature.

By the end of this lab you will be able to:

  • Show why name resolution fails on the default bridge network
  • Resolve containers by name on a user-defined network, even after a restart
  • Use Docker Compose service names as hostnames
  • Add network aliases and see how multiple containers share one name
  • Limit which containers can resolve each other with separate networks
  • Map each Docker DNS idea to its Kubernetes equivalent

Why This Matters in Production

Container IP addresses are assigned at start and can change on every restart, so hardcoding them in configuration eventually causes an outage. Names stay stable while the IPs behind them change, which is why every orchestrator provides some form of service discovery.

Imagine a payment-api container that connects to postgres using the address 172.18.0.3. After a host reboot the database starts first and takes a different address, and the API fails with connection timeouts until someone edits the configuration. Using the name postgres removes that failure. This scenario is illustrative, not a report of a specific incident.

Core Principles

How Docker Resolves a Container Name

A container on a user-defined network sends DNS queries to 127.0.0.11. Docker answers names it knows (container names, Compose service names and aliases on networks the container shares) and forwards everything else to the host's upstream resolvers.

flowchart TD A["App calls
db:5432"] --> B["Query sent to
127.0.0.11"] B --> C{"Name known on a
shared network?"} C -->|Yes| D["Return container IP"] C -->|No| E["Forward to host
upstream DNS"] classDef app fill:#DBEAFE,color:#1E293B classDef tool fill:#326CE5,color:#FFFFFF classDef good fill:#D1FAE5,color:#1E293B classDef warn fill:#FFEDD5,color:#1E293B class A app class B tool class C warn class D,E good
Default Bridge vs User-Defined Network

The default bridge network is a legacy mode with no name resolution, while user-defined networks add automatic DNS and better isolation.

Feature Default bridge User-defined network
Resolve by container name No Yes
DNS server in container Host's DNS settings 127.0.0.11
Isolation between groups All containers share it Only members can talk
Remember

Containers talk to each other on the container's own port. A published port (-p 8080:80) only matters for traffic coming from the host or outside.

Detailed Step-by-Step Practical Lab

Step 1 - Why Containers Cannot Find Each Other by Name on the Default Bridge

On the default bridge network, name lookups fail because there is no embedded DNS. Prove it with two throwaway containers.

Bash
docker run -d --name api-default alpine:3.20 sleep 3600
docker run -d --name db-default alpine:3.20 sleep 3600
docker exec api-default ping -c 1 db-default
docker exec api-default cat /etc/resolv.conf
# Shows the host's DNS servers, not 127.0.0.11
docker rm -f api-default db-default
Step 2 - How to Resolve Containers by Name on a User-Defined Network

Create a network, attach both containers to it, and look up the database by name. Note that -e must come before the image name, or Docker passes it to the program as an argument.

Bash
docker network create app-network
docker run -d --name db --network app-network \
-e POSTGRES_PASSWORD=mumbai-dev-secret \
postgres:16
docker run -d --name api --network app-network alpine:3.20 sleep 3600
docker exec api cat /etc/resolv.conf
# nameserver 127.0.0.11
# options ndots:0
docker exec api nslookup db
# Server: 127.0.0.11
# Name: db
# Address: 172.18.0.2

Restart the database and look it up again. The name keeps working even if the IP changes.

Bash
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db
docker restart db
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db
docker exec api nslookup db
# Returns whatever address db has now
Step 3 - How Docker Compose Service Names Work as Hostnames

Compose creates a user-defined network for each project and registers every service name on it. The network is named <project>_default, and the project name defaults to the directory name. The version: key is obsolete in current Compose and can be left out.

YAML
# docker-compose.yml
services:
payment-api:
image: registry.razorpay.in/payment-api:v3.1.0
environment:
DB_HOST: postgres # service name used as hostname
REDIS_HOST: redis
QUEUE_HOST: rabbitmq
postgres:
image: postgres:16
environment:
POSTGRES_PASSWORD: mumbai-dev-secret
redis:
image: redis:7-alpine
rabbitmq:
image: rabbitmq:3-management
Bash
docker compose up -d
# Resolve a service name from inside another service
docker compose exec payment-api getent hosts postgres
# See the network Compose created and which containers joined it
docker network ls
docker network inspect payment_default
Step 4 - How to Add Network Aliases

A network alias gives a container extra names on one network. It is useful when applications expect a fixed hostname such as db, or when you want to switch which container answers to a name.

Bash
docker run -d --name postgres-primary --network app-network \
--network-alias db-primary \
--network-alias database \
-e POSTGRES_PASSWORD=mumbai-dev-secret \
postgres:16
docker exec api nslookup database
YAML
# The same in docker-compose.yml
services:
postgres-primary:
image: postgres:16
networks:
app-network:
aliases:
- db-primary
- database
networks:
app-network:
Step 5 - How Docker DNS Balances Between Several Containers

When several containers share one name or alias on a network, Docker DNS returns all of their IPs and rotates the order. This is simple round-robin DNS, not a health-aware load balancer.

Bash
docker compose up -d --scale worker=3
docker compose exec payment-api nslookup worker
# Returns three addresses, one per worker container

Clients that cache the first answer will not spread traffic evenly, so use a reverse proxy when you need real load balancing.

Step 6 - How to Limit Which Containers Can Resolve Each Other

A container resolves only the names on networks it has joined. Use two networks so the web tier can reach the API, and only the API can reach the database.

YAML
services:
web:
image: nginx:1.27-alpine
networks: [frontend]
payment-api:
image: registry.razorpay.in/payment-api:v3.1.0
networks: [frontend, backend]
postgres:
image: postgres:16
environment:
POSTGRES_PASSWORD: mumbai-dev-secret
networks: [backend]
networks:
frontend:
backend:
internal: true # no outbound internet from this network

From web, the name postgres does not resolve, because they share no network.

To let services in different Compose projects talk, create a network once and mark it external: true in both files:

Bash
docker network create shared-network
YAML
services:
payment-api:
image: registry.razorpay.in/payment-api:v3.1.0
networks: [shared-network]
networks:
shared-network:
external: true
Step 7 - How to Debug a Name That Will Not Resolve

Work from the container outward and stop at the first failure.

  1. Run docker exec <container> cat /etc/resolv.conf. If it does not show 127.0.0.11, the container is on the default bridge.
  2. Run docker inspect <container> and check Networks. Both containers must share a user-defined network.
  3. Run docker exec <container> nslookup <name>. Check the spelling and the Compose service name.
  4. Test the port, not just the name, since a successful lookup does not prove the service is listening.
Tip

To override upstream DNS for one container, use docker run --dns 10.0.0.2 .... Internal names still resolve through 127.0.0.11, and only external lookups use the server you set.

How This Maps to Kubernetes

Kubernetes provides the same idea through CoreDNS, with Services as the stable name and namespaces as the scope.

Docker Kubernetes
Embedded DNS at 127.0.0.11 CoreDNS, reached through the kube-dns Service
Compose service name Service name
User-defined network Namespace plus network policy
Network alias Additional Service or ExternalName
Short name db db, or db.<namespace>.svc.cluster.local

One difference to remember: Kubernetes pods search several domains and use ndots:5, so short names can trigger extra lookups, while Docker uses ndots:0.

Production Best Practices & Common Pitfalls

  • Never hardcode container IPs. Use names or service names so restarts and redeployments do not break connections.
  • Always use user-defined networks. Even for a single docker run, create a network first. The default bridge has no DNS.
  • Use one network per trust boundary. Separate frontend and backend networks limit what a compromised container can reach.
  • Do not expect DNS load balancing. Round-robin DNS gives no health checks, and clients may cache one address.
  • Remember --link is legacy. It only exists for the default bridge and should not be used in new setups.
  • Choose names deliberately. Container names and aliases must be unique per network, and a clash makes names resolve unpredictably.

Quick Reference & Troubleshooting Commands

Command Description
docker network create NAME Create a user-defined bridge network
docker network connect NET CONTAINER Attach a running container to a network
docker network inspect NET Show subnet, gateway and attached containers
docker exec C cat /etc/resolv.conf Check which DNS server the container uses
docker exec C nslookup NAME Test name resolution from inside a container
docker compose up -d --scale SVC=3 Run several replicas behind one service name
Symptom Cause Fix
bad address 'db' Containers on the default bridge, or different networks Put both on one user-defined network
Name resolves, connection times out Wrong port or service not listening Test the container port with nc or curl
Old IP returned after restart Application cached the address Restart the client or fix its DNS caching
External names fail, internal ones work Host DNS or --dns server unreachable Check the host resolver and the --dns setting
Compose service not found from another project Different networks per project Use a shared external network

Resources

Explore More in Docker Networking and Storage

All 5 Topics

Frequently Asked Questions

Is Understanding Docker DNS and Service Discovery free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Understanding Docker DNS and Service Discovery topic cover?

Learn how Docker's embedded DNS lets containers find each other by name on user-defined networks and Compose, and how it maps to Kubernetes DNS.

Found a mistake, an outdated step or a broken link?