7.3.2.4 Compose DNS Naming
A focused guide to Compose DNS Naming, connecting core concepts with practical Docker and container operations.
Compose DNS naming describes how Docker Compose automatically makes each service in a docker-compose.yml file resolvable by its defined service name, building on the same underlying user-defined network DNS resolution Docker provides, but configured automatically rather than requiring manual network and naming setup.
How Compose Sets This Up Automatically
Compose automatically creates a dedicated network for the project and attaches every defined service to it, with each service's name (as defined in the Compose file) becoming automatically resolvable.
services:
api:
image: myapi:1.0
db:
image: postgres:16
docker compose up -d
docker compose exec api ping db
This succeeds without any manual network creation, since Compose handled both network creation and service attachment automatically based on the service names defined in the file.
Why the Compose Service Name Is What Matters, Not the Container Name
Compose-managed containers often have generated names (typically combining the project and service name with a number), but it's the defined service name itself — db in this example — that is automatically resolvable, regardless of the underlying container's actual generated name.
docker compose ps
This reveals the actual generated container names, which may differ from the simpler service name used for DNS resolution between services.
Scaling a Service While Preserving Resolution
When a service is scaled to multiple replicas, the service name continues resolving correctly, now potentially returning multiple addresses corresponding to the multiple running replicas.
docker compose up -d --scale worker=3
Why Compose DNS Naming Matters
Compose's automatic handling of network creation and name-based resolution removes the need to manually replicate this setup for every multi-container project, making correctly functioning service-to-service communication the default, expected behavior for any properly defined Compose application.