Sub topic 11
How Dependencies Affect Shutdown Order
This chapter assumes you can read a compose.yaml file, run docker compose up and docker compose down, and use depends_on in its short and long syntax. You should also know that a started container is not necessarily ready.
Shutdown is the reverse of startup
Compose uses the depends_on graph in both directions. On startup, a dependency is created and started before the services that need it. On shutdown, Compose reverses the order. It stops the dependents first and the dependencies last.
Take this file:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
web:
image: myapp:latest
depends_on:
- dbWhen you run docker compose down or docker compose stop, Compose stops web first. It waits for web to exit, then stops db. This lets the application close its database connections and finish in-flight work while the database is still running. If db stopped first, web could log connection errors or lose writes during its own shutdown.
What the ordering covers
The ordering applies to the commands that stop or remove services:
docker compose stopanddocker compose downstop dependents before dependencies.docker compose restartfollows the same dependency ordering for the services it restarts.docker compose downremoves containers after they stop, and then removes networks.
Conditions do not change this. condition: service_healthy and condition: service_completed_successfully affect only startup. At shutdown, Compose does not wait for a health state. It sends the stop signal to dependents, waits for them to exit, and then moves on to their dependencies.
The stop signal and the grace period
For each container, Compose sends SIGTERM and waits up to a grace period. If the container is still running when the period ends, Compose sends SIGKILL. The default grace period is 10 seconds. You can change it per service with stop_grace_period:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
web:
image: myapp:latest
depends_on:
- db
stop_grace_period: 30sThe order only helps if web handles SIGTERM. Many applications run behind a shell script, and the shell may not pass the signal on. In that case web ignores SIGTERM, waits out the full grace period, and is then killed. Compose still stops db only after web is gone, so the ordering holds, but every shutdown is slow and the application never gets to clean up.
Common errors
- Slow shutdowns of exactly 10 seconds. The container is not handling
SIGTERM. Run the application as PID 1 using the exec form ofcommandorENTRYPOINT, or addinit: trueto the service. - Expecting order between unrelated services. Services with no
depends_onlink between them have no guaranteed stop order. Compose can stop them at the same time. - Stopping one service by name.
docker compose stop dbstopswebfirst becausewebdepends ondb. This is expected, not a bug. - Assuming data is safe. Stopping the application first avoids new writes, but it does not replace a clean database shutdown or a backup.
Checking the order
Run the stack and watch the output of down:
docker compose up -d
docker compose downThe down output lists web as stopped and removed before db. If you want to see the timing, run docker compose down --timeout 5 to shorten the grace period for that command.
Exercise
A stack has web depending on db. You run docker compose stop db. Which containers does Compose stop, and in what order? Why does a web container that ignores SIGTERM make this command slow?
Show answer
Compose stops web first and then db, because dependents are stopped before the services they depend on. If web ignores SIGTERM, Compose waits for the full grace period (10 seconds by default) and then sends SIGKILL. Only after web is gone does it stop db, so the whole command takes at least that long.