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.
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 |
RememberContainers 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.
docker run -d --name api-default alpine:3.20 sleep 3600docker 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-defaultStep 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.
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.2Restart the database and look it up again. The name keeps working even if the IP changes.
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dbdocker restart dbdocker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db docker exec api nslookup db# Returns whatever address db has nowStep 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.
# docker-compose.ymlservices: 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-managementdocker compose up -d # Resolve a service name from inside another servicedocker compose exec payment-api getent hosts postgres # See the network Compose created and which containers joined itdocker network lsdocker network inspect payment_defaultStep 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.
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# The same in docker-compose.ymlservices: 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.
docker compose up -d --scale worker=3 docker compose exec payment-api nslookup worker# Returns three addresses, one per worker containerClients 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.
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 networkFrom 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:
docker network create shared-networkservices: payment-api: image: registry.razorpay.in/payment-api:v3.1.0 networks: [shared-network] networks: shared-network: external: trueStep 7 - How to Debug a Name That Will Not Resolve
Work from the container outward and stop at the first failure.
- Run
docker exec <container> cat /etc/resolv.conf. If it does not show127.0.0.11, the container is on the default bridge. - Run
docker inspect <container>and checkNetworks. Both containers must share a user-defined network. - Run
docker exec <container> nslookup <name>. Check the spelling and the Compose service name. - Test the port, not just the name, since a successful lookup does not prove the service is listening.
TipTo override upstream DNS for one container, use
docker run --dns 10.0.0.2 .... Internal names still resolve through127.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
--linkis 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 |