Sub topic 2
Controlling start order with depends_on
This chapter assumes you can write a compose.yaml with services, images, and environment variables, and that you can run docker compose up and docker compose down.
What depends_on does
By default, Compose starts all services at about the same time. The depends_on attribute changes that. It tells Compose that one service needs another, so Compose creates and starts the dependency first.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
web:
image: myapp:latest
depends_on:
- dbThis is the short syntax. It is a list of service names. Compose creates and starts db, then creates and starts web.
Dependencies start with the service
Start order is not the only effect. When you start a service, Compose also starts everything it depends on. This command starts both db and web:
docker compose up -d webYou do not need to name db. Compose follows the depends_on links and includes it.
Chains and multiple dependencies
A service can depend on several others, and dependencies can form a chain. Compose works out the order from the whole graph.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
cache:
image: redis:7
api:
image: myapp:latest
depends_on:
- db
- cache
proxy:
image: nginx:stable
depends_on:
- apiHere db and cache have no dependencies, so Compose can start them in parallel. api starts after both. proxy starts last.
What the short syntax guarantees
The short syntax guarantees one thing: Compose has started the dependency's container before it starts the dependent service. It does not check whether the software inside is ready. A PostgreSQL container may take several seconds after it starts before it accepts connections. If web connects in that window, the connection fails.
The next chapters cover this gap. For now, treat short depends_on as ordering only.
Common errors
- Undefined service. Naming a service that is not in the file fails with an error, such as
depends on undefined service. Check for typos. - Circular dependency. If
adepends onbandbdepends ona, Compose cannot find a start order. It reports a dependency cycle. Remove one link, or move the shared piece into a third service. - Assuming it controls networking.
depends_ondoes not create network links. Services on the same Compose network reach each other by service name whether or notdepends_onis set. - Assuming it retries. If a dependent service crashes because its dependency was not ready, Compose does not restart it unless you set a
restartpolicy.
Shutdown order
Dependencies also affect shutdown. docker compose down stops services in reverse order, so web stops before db. This gives the application a chance to finish work while its database is still running.
Exercise
A compose file has web with depends_on: [db]. You run docker compose up -d web. Which containers start, in what order, and does Compose confirm that the database accepts connections before starting web?
Show answer
Compose starts db first and then web, because it includes dependencies of the named service. It does not confirm that the database accepts connections. The short syntax only guarantees that the db container has been started, not that PostgreSQL is ready.