Lesson 7 of 9 · 10 min · Container networking

Container DNS and networks in Compose

Covers: Docker Engine 29 and Compose 5 on Ubuntu 24.04. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • How name resolution works inside user-defined networks
  • How Compose names networks and services, and what a service name resolves to
  • Isolating front end, back end and database with several networks
  • Network aliases, and DNS round robin with replicas
  • A method to debug "cannot connect" between containers

In simple words

A container's address changes every time it restarts, so you shouldn't use addresses at all. Docker gives each container a name that the others can call, like saving a contact in your phone instead of memorising the number. This lesson shows how those names work and how to keep the database away from the public side.

Words to know

  • Service name: the name other containers use to reach a container, such as db.
  • User-defined network: a network you create. Containers on it can find each other by name.
  • Isolation: putting containers on separate networks so that only the right ones can talk.
  • Alias: an extra name for the same container.

Docker's embedded DNS

On a user-defined network, every container gets /etc/resolv.conf pointing at Docker's embedded DNS server, always at 127.0.0.11 (an address inside each container). Our container showed:

nameserver 127.0.0.11
options edns0 trust-ad ndots:0

That resolver answers questions about other containers on the same network (by container name, by network alias and, in Compose, by service name) and forwards everything else to the host's DNS servers, so containers can also resolve public names. A lookup in our test:

docker exec b1 nslookup b2
Address: 172.30.0.3

Properties to remember:

  • Names only resolve within the same network: a container on network A cannot resolve a name that exists only on network B, which is a feature (isolation).
  • Names resolve to current IPs. When a container restarts and gets a new IP, the name still works for new lookups. Applications that cache DNS forever (some JVM configurations, long-lived connection pools, nginx upstream names resolved at start-up) can keep the old address: restart them, use a resolver directive, or configure DNS TTLs.
  • The default bridge network has no such DNS (previous lesson).
  • ping may be missing in minimal images; use getent hosts name, nslookup, or wget/curl to test.

Networks in Compose

Compose creates one network per project, named <project>_default, and attaches every service to it. In our stack the network comp_default (driver bridge) was created at up and removed at down. Each service is reachable by its service name:

docker compose exec web python -c "import socket; print('db ->', socket.gethostbyname('db'))"
db -> 172.19.0.2

So the application's configuration says DB_HOST=db, not an IP address and not localhost. The same Compose file works on every machine, since only the service name matters.

Use the container port between services: db:5432, even if you published a different port on the host (15432:5432). Published ports are only for clients outside Docker.

Several networks: isolating tiers

By default all services can talk to each other. For least privilege, define networks per tier and attach each service only where needed:

services:
  proxy:
    image: nginx:1.27-alpine
    ports: ["80:80"]
    networks: [frontend]
  api:
    build: ./api
    networks: [frontend, backend]
  db:
    image: postgres:16-alpine
    networks: [backend]
networks:
  frontend:
  backend:
    internal: true
  • proxy can reach api (same frontend network) but cannot see db at all: it is not even resolvable from there.
  • api is on both networks and is the only bridge between them.
  • internal: true makes the backend network have no route to the outside world: the database cannot make outbound connections, and nobody outside can reach it.

We tested exactly this layout: proxy resolved api but getent hosts db failed with no answer; api resolved both db and its alias database to the same address; a wget from db to a public site failed with bad address because the internal network has no route out, while api (also on the normal frontend network) reached the internet. After --scale api=3, resolving api returned all three replica addresses. This is a cheap, effective security boundary, and mirrors the subnet tiers of a cloud VPC (Networking course).

Aliases and replicas

A service can have extra names with aliases under the network entry:

networks:
  backend:
    aliases: [database]

When a service runs several replicas (docker compose up --scale api=3), the service name resolves to all replica IPs, in rotating order: DNS round robin. Clients that resolve once per connection spread load roughly evenly; those that cache one address do not. For proper load balancing use a reverse proxy or the orchestrator's service abstraction.

Attaching to an existing network

To make a Compose project join a network created elsewhere (for example a shared network with a reverse proxy container):

networks:
  default:
    name: shared
    external: true

Create it once with docker network create shared.

Debugging container connectivity

Work from the inside, in the same order as the Linux and Networking courses:

  1. Is the target container running and healthy? docker compose ps, docker compose logs db.
  2. Same network? docker network inspect NETWORK lists the attached containers; a name that is missing from the list will not resolve.
  3. Does the name resolve? docker compose exec web getent hosts db. If it fails: wrong service name, different network, or the container is not running.
  4. Is the port reachable? From the client container: nc -zv db 5432, or wget -qO- http://api:8000/health, or Bash's </dev/tcp/db/5432. "Connection refused" means the service is not listening on that port on that interface: many servers listen on 127.0.0.1 by default inside the container, which blocks other containers. Bind to 0.0.0.0.
  5. Is the app using the right name and port? Not localhost, not the host-published port.
  6. Firewall-like causes: an internal network blocking egress, Kubernetes network policies, or host firewall rules affecting the bridge.
  7. No tools in the image? Attach a debug container to the same network or namespace: docker run --rm -it --network container:web nicolaka/netshoot and run dig, curl, tcpdump there.

A typical failure: the web container starts before the database is ready and exits with "connection refused" or "name does not resolve". Add a health check with depends_on: condition: service_healthy and connection retries in the app.

Common mistakes

  • Using localhost for another service.
  • Using the published host port between containers.
  • Servers bound to 127.0.0.1 inside the container.
  • Expecting name resolution across different networks.
  • Hard-coding container IPs.
  • Forgetting that long-lived clients cache DNS.
  • Putting everything on one network and exposing the database to every service.

Try it yourself

  1. Start a Compose project with a web and a database service; use getent hosts db inside the web container, then change the service name and observe what breaks.
  2. Split the stack into frontend and backend networks as above and prove the proxy cannot resolve the database.
  3. Mark backend as internal: true and verify that a container on it cannot reach the internet (wget to a public URL).
  4. Scale a service to three replicas and resolve its name several times; note the order of returned addresses.

Check yourself

What is 127.0.0.11 in a container?

Docker's embedded DNS resolver for user-defined networks. It answers for container, alias and service names on the container's networks and forwards other queries to the host's DNS servers.

What does the name db in DB_HOST=db resolve to in Compose?

The IP address of the db service's container on the shared Compose network (all replica IPs if scaled).

Why is it better to use several Compose networks than the default one?

It limits which services can reach each other (least privilege). A front-end proxy can be kept unable to see the database at all, and an internal network has no outside access.

A container can resolve db but gets connection refused on port 5432. What next?

Check that the database is listening on the container's network interface rather than only 127.0.0.1, that it is fully started (health check), and that you use the container port, not a published host port.

Interview questions

How do containers discover each other in Docker?

Through Docker's embedded DNS on user-defined networks (container names, aliases, Compose service names). In Kubernetes the equivalent is cluster DNS and Services.

How would you make a database unreachable from the internet and from the front-end tier, but reachable from the API?

Put the database only on a back-end network shared with the API (and mark it internal), publish no database ports, and attach the proxy only to the front-end network.

Why might a service keep connecting to an old IP after a container is replaced?

The client cached the DNS result (JVM, connection pool, nginx resolving upstream at start-up). Restart it or configure DNS refresh/TTL handling.

Summary

  • Docker's embedded DNS (127.0.0.11) resolves container and service names on user-defined networks; names are per network.
  • Compose gives each project a default network where service names are hostnames; use the container port, never localhost or the published port.
  • Multiple networks (with internal: true) isolate tiers; replicas resolve to several IPs.
  • Debug in order: running, same network, name resolution, port, bind address, application settings.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Docker and containers for DevOps Engineer and scores every answer, with what to fix.