Sub topic 2
Anatomy of a compose.yaml
This chapter assumes you can read YAML, know what a container and an image are, and have Docker Engine or Docker Desktop with the docker compose plugin installed. It covers the top-level layout of a Compose file. Later chapters go deeper into each section.
The file and its name
Compose looks for compose.yaml in the current directory. It also accepts compose.yml, docker-compose.yaml, and docker-compose.yml, but compose.yaml is the preferred name. The file describes one application, which Compose calls a project.
Top-level elements
A Compose file is a YAML mapping. These top-level keys are the ones you will use most:
| Key | Purpose |
|---|---|
| `name` | Sets the project name. |
| `services` | Defines the containers to run. |
| `networks` | Defines custom networks. |
| `volumes` | Declares named volumes. |
| `secrets` | Declares secrets (covered in a later chapter). |
| `configs` | Declares configs (covered in a later chapter). |
Only services is required. The older version: key is obsolete. Current Compose ignores it and prints a warning, so leave it out.
A worked example
This file defines a web application and its database.
name: shop
services:
web:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
volumes:
db-data:Read it from the top down:
name: shopsets the project name. Compose prefixes the names of the containers, networks, and volumes it creates with it, for exampleshop-web-1. Withoutname, Compose uses the directory name.servicesholds one entry per service. The key, such aswebordb, is the service name. Other services use that name as a hostname.- Each service sets either
image:to pull a prebuilt image orbuild:to build from a Dockerfile.webbuilds from the current directory, anddbpullspostgres:16. - The top-level
volumessection declaresdb-data. The service then refers to it by name in its ownvolumeslist.
Declaring versus using
Resources such as volumes and networks work in two steps. You declare them at the top level, then reference them from a service. A name used in a service but missing from the top level is an error for volumes. The exception is a bind mount, which uses a host path like ./src and needs no declaration.
Networks are different. If you declare none, Compose creates a default network for the project and attaches every service to it. This is why web can reach the database at the hostname db with no network configuration.
Checking your file
Run docker compose config to validate the file. It prints the fully resolved configuration, with variables filled in and short syntax expanded. It exits with an error if the file is invalid.
docker compose configIndentation errors are the most common mistake. YAML uses spaces, never tabs, and a service key indented at the wrong level is read as a property of the previous service.
Exercise
In the example file, why does the web service use db as the hostname in DATABASE_URL, and which part of the file makes the db-data volume usable by the db service?
Show answer
Compose creates a default network and registers each service name as a DNS name on it, so db resolves to the database container. The db-data volume is declared in the top-level volumes section and then referenced in the db service's own volumes list as db-data:/var/lib/postgresql/data.