Sub topic 7
The long syntax and its conditions
This chapter assumes you can read a compose.yaml file and know what a healthcheck block does. The short depends_on list only controls start order. The long syntax lets you say what each dependency must be doing before the dependent service starts.
From list to map
The short syntax is a list of service names. The long syntax is a map. Each dependency name becomes a key, and its value holds a condition.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
web:
image: myapp:latest
depends_on:
db:
condition: service_startedThis file behaves the same as the short form. service_started is the default condition, so it only waits for the db container to be started.
You cannot mix the two forms inside one depends_on block. Choose a list or a map. If one dependency needs a condition, write all of them as map entries.
The three conditions
| Condition | Compose waits until | Typical use |
|---|---|---|
| `service_started` | The dependency container has started | Services with no readiness concerns |
| `service_healthy` | The dependency's healthcheck reports healthy | Databases, caches, message brokers |
| `service_completed_successfully` | The dependency has exited with code 0 | Migrations, seed scripts, setup jobs |
The first condition is the same guarantee as the short syntax. The other two need something to observe. service_healthy needs a healthcheck on the dependency. service_completed_successfully needs a container that is meant to exit.
Combining conditions
A service can have several dependencies, each with its own condition. In this example web needs a healthy cache and a finished migration:
services:
cache:
image: redis:7
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
migrate:
image: myapp:latest
command: ["./migrate"]
web:
image: myapp:latest
depends_on:
cache:
condition: service_healthy
migrate:
condition: service_completed_successfullyCompose starts web only after every listed condition is true. The cache and migrate services have no dependency on each other here, so Compose can start them at the same time.
What happens when a condition fails
A condition that can never be met stops the start-up. It does not hang forever.
- If the dependency becomes
unhealthy,docker compose upstops with an error and the dependent service is not started. - If a
service_completed_successfullyjob exits with a non-zero code, the dependent service is not started and the command fails. - If the dependency has no
healthcheckand you useservice_healthy, Compose rejects the configuration or reports the missing check. Add the check to the dependency's definition, not to the dependent service.
The conditions only apply when Compose starts the dependent service. If db becomes unhealthy later, Compose does not stop or restart web.
Common mistakes
- Mixing list and map entries. Convert the whole block to the map form.
- Putting the healthcheck on the wrong service. The
healthcheckbelongs to the service that is waited on, not to the one that waits. - Using
service_completed_successfullyon a long-running service. A web server never exits, so the dependent service waits indefinitely or fails. - Expecting
service_healthyto restart or repair anything. It only gates the first start.
Exercise
A service api must start only after db is healthy and after a one-shot seed service has exited with code 0. Write the depends_on block for api.
Show answer
dependson:
db:
condition: servicehealthy
seed:
condition: servicecompletedsuccessfully
The db service also needs a healthcheck, and seed must be a container that exits when its work is done.