Sub topic 9
Running migrations first with service_completed_successfully
A database that is healthy is still often empty. Before the application can serve requests, something has to create the tables. This chapter shows how to run that work as a one-shot service and make the application wait for it.
This chapter assumes you can read a compose.yaml file, know what depends_on and condition: service_healthy do, and can run docker compose up and docker compose down.
Why a separate migration service
You could run migrations inside the application's start command. That has two drawbacks. If you scale web to three replicas, all three try to migrate at once. A migration failure also leaves a running container that is half broken.
A one-shot service solves both. It runs the migration command, then exits. Its exit code tells Compose whether the job worked.
The condition
depends_on accepts three conditions in the long syntax:
| Condition | The dependent service starts when |
|---|---|
| `service_started` | The dependency container has started. This is the default. |
| `service_healthy` | The dependency's healthcheck reports healthy. |
| `service_completed_successfully` | The dependency has exited with code 0. |
The third condition is meant for jobs that are supposed to finish. A normal long-running service never "completes", so it is the wrong choice for a database or web server.
A complete example
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
migrate:
image: myapp:latest
command: ["./migrate"]
depends_on:
db:
condition: service_healthy
web:
image: myapp:latest
depends_on:
migrate:
condition: service_completed_successfullyThe chain has three links:
dbstarts and runs its healthcheck until it reports healthy.migratestarts only afterdbis healthy. It runs./migrateand exits.webstarts only ifmigrateexited with code 0.
What happens on failure
If migrate exits with a non-zero code, Compose does not start web. The docker compose up command reports that the dependency failed. This is the behavior you want: the application never runs against a half-migrated schema.
To find out why, read the job's logs and its exit code:
docker compose logs migrate
docker compose ps -aUse -a because docker compose ps hides stopped containers by default, and a finished job is a stopped container.
Common mistakes
- Giving the job a restart policy. With
restart: always, the container restarts after it exits, so it never stays "completed". Leave the restart policy unset, or useno, on one-shot jobs. - Skipping the database wait. If
migratedepends ondbwith the short syntax, it may start before PostgreSQL accepts connections and fail. Givemigratetheservice_healthycondition ondb. - Non-idempotent migrations. On the next
docker compose up, themigratecontainer runs again. Your migration tool must tolerate being run on an already migrated database. Most tools track applied versions and do nothing.
Exercise
In the example, web should not start if ./migrate fails. Which setting enforces this, and what exit code counts as success?
Show answer
condition: service_completed_successfully under web's depends_on entry for migrate. Exit code 0 counts as success. Any other code stops web from starting.
Exercise
Your migrate service has restart: always, and web never starts. Why?
Show answer
The restart policy restarts migrate every time it exits, so Compose never sees it in a completed state. Remove the restart policy.