Startup order: depends_on and health checks
depends_on orders container creation, not readiness. Health checks, service_healthy, one-shot migrations and why your app must still retry.
The Compose file: services, networks and volumesRestart policies and health checks
Startup order: depends_on and health checks
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Short-form `depends_on` only orders creation and shutdown. Compose waits until a dependency is running, not until it is ready to accept connections.
- The long form adds a `condition`: `service_started`, `service_healthy` or `service_completed_successfully`.
- `service_healthy` needs a `healthcheck` on the dependency. The check runs inside that container, so its command must exist in the image.
- Health check timing matters. With default settings the first check runs 30 seconds after start, so set `start_period` and `start_interval` to get a fast first answer.
- `service_completed_successfully` runs a one-shot container, such as a migration, to exit 0 before dependents start. Compose 5.3 and later also offer `pre_start` init containers.
- `restart: true` on a dependency restarts the dependent when Compose restarts or updates the dependency. `required: false` turns a missing dependency into a warning.
- Startup order only covers the first start. Databases restart and networks blip later, so the application must still retry its connections.
Running is not ready
The first docker compose up of a new project often ends like this: the database container starts, the API container starts a second later, the API tries to connect, gets connection refused, and exits. Run up again and it works, because the database had warmed up in the meantime.
The cause is a gap between two states. A container is running once its main process has started. A database is ready once it has replayed its log, opened its socket and started accepting connections. Postgres can take several seconds between the two, longer on first start while it initialises a data directory. Compose, by default, only waits for the first.
What depends_on actually does
The short form:
services:
api:
build: .
depends_on: [db]
db:
image: postgres:18
The Compose reference says this does three things: Compose creates db before api, removes api before db, and guarantees db has been started before starting api. It also says, plainly, that with the short syntax Compose does not wait for a dependency to be healthy. It is a creation order, like a recipe telling you to turn the oven on before mixing the batter. It does not tell you the oven is hot.
Waiting for healthy
The long form adds a condition. To wait for real readiness, combine it with a health check on the dependency:
services:
api:
build: .
depends_on:
db:
condition: service_healthy
restart: true
db:
image: postgres:18
environment:
POSTGRES_PASSWORD: dev
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER:-postgres}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
start_interval: 1s
The health check is a command Docker runs inside the db container. Exit 0 means healthy, 1 means unhealthy. pg_isready ships in the Postgres image, so it works here. A check like curl -f http://localhost/health fails on an image that has no curl, which is the usual reason a check never passes on a slim or distroless image.
Notice $$. Compose interpolates $VAR on your machine when it reads the file. $$ escapes that, so $POSTGRES_USER reaches the container and its shell expands it there. When I loaded a file with $$HOSTNAME in a health check through compose-go, the library the Compose CLI uses, the resolved command contained a single $HOSTNAME, as expected.
The timing trap
The Dockerfile reference gives the defaults: interval 30s, timeout 30s, retries 3, start_period 0s, start_interval 5s. The first check runs one interval after the container starts. So a database with a health check and no other settings is not “healthy” for at least 30 seconds, and your API waits at least that long on every up.
start_period is a grace window during which failures don’t count, and start_interval (Docker Engine 25 or later) is how often to check during it. With start_period: 30s and start_interval: 1s, the database is checked every second at startup and reported healthy within a second of being ready, while steady-state checks stay at every 10 seconds.
If the dependency never becomes healthy, Compose stops and reports it. The Compose source has the messages you’ll see, such as dependency failed to start: container db is unhealthy.
Running something once first
Migrations are the classic case: the schema must be updated before the API boots, exactly once per deploy.
services:
migrate:
image: registry.example.com/api:1.4.2
command: ["./manage.py", "migrate"]
depends_on:
db:
condition: service_healthy
api:
image: registry.example.com/api:1.4.2
depends_on:
migrate:
condition: service_completed_successfully
service_completed_successfully waits for migrate to exit with code 0. A failed migration stops api from starting, which is what you want.
Compose 5.3 added a tidier form for this: pre_start init containers on the service itself. Each step runs in its own short-lived container from the service’s image, on its networks and volumes, before the service’s main container starts. The docs note a step is skipped on later up runs if it already succeeded and its definition hasn’t changed.
services:
api:
image: registry.example.com/api:1.4.2
depends_on:
db:
condition: service_healthy
pre_start:
- command: ["./manage.py", "migrate"]
The two other knobs
restart: trueon a dependency entry: when Compose restarts or updatesdb, for example ondocker compose restart dbor a changed image, it restartsapitoo, so the API reconnects. It does not apply when the engine restarts a crashed container on its own.required: false: if the dependency is missing or fails, Compose warns and carries on. Useful for optional sidecars.
To make scripts and CI wait until everything is up, docker compose up --wait blocks until services are running or healthy, with --wait-timeout as a limit.
Order only covers the first start
All of this happens once, at up. Tomorrow the database restarts for an upgrade or runs out of memory, and nothing reorders anything. An application that only connects at startup breaks then. The fix lives in the application: retry with backoff, and use a connection pool that reconnects. The backend roadmap covers this in health checks and graceful shutdown. Treat depends_on as a convenience for the first start, not a guarantee.
About the video
The embed is all of WittCode’s “Docker Compose Control Container Order with depends_on”, April 2024, 4 minutes 17 seconds. Its chapters walk the same path as this lesson’s core: the short depends_on syntax, the long syntax with service_healthy, then the healthcheck that makes it work, using a Node app and Postgres. It is short and doesn’t cover start_interval, restart: true, required, one-shot migrations or pre_start, which the lesson adds. English is confirmed by YouTube’s “English (US) original” audio flag. Nana’s chapter on startup order in deeper is a second look at the same idea.
What was run: a Compose file with $$ escaping was loaded with compose-go v2.17.0 (the library inside Compose v5.8.1) to confirm the escaping, and Compose’s error messages were read from its v5.8.1 source. No daemon was available, so no containers were started. Health check defaults are from the Dockerfile reference and the rest from the Compose documentation, checked 2026-11-28.
Where this fits
Your file now starts in the right order. Next, making the same file work across machines and environments: variables, env files and profiles.
Check yourself
web has depends_on: [db] in short form, yet it crashes with connection refused on the first docker compose up. Why?
Short-form depends_on only waits for the db container to be running. Postgres needs a few seconds to initialise before it accepts connections, and web connected during that window.
You add condition: service_healthy, but up now fails with container db has no healthcheck configured. What is missing?
A healthcheck on the db service (or a HEALTHCHECK in its image). service_healthy can only wait for a health status that exists.
A migrate service runs once and exits. How do you make api start only after it succeeds?
Give api depends_on: migrate with condition: service_completed_successfully. If migrate exits non-zero, api is not started. On Compose 5.3 and later, a pre_start step on api is an alternative.
Why does a healthcheck written as pg_isready -U $POSTGRES_USER in compose.yaml often need $$?
Compose interpolates $VAR from the host side when it reads the file. $$ escapes it, so the literal $POSTGRES_USER reaches the container and the shell there expands it from the container's environment.
Everything started in order on Monday. On Tuesday the database restarted and the API began failing until someone restarted it. What does that tell you?
The API only connects at startup and never reconnects. depends_on can't help after the first start; the app needs retries or a connection pool that reconnects.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.