depends_on
expresses startup and shutdown dependencies between services.
The short syntax variant only specifies service names of the dependencies. Service dependencies cause the following behaviors:
- Compose creates services in dependency order. In the following example,
db
and redis
are created before web
.
- Compose removes services in dependency order. In the following example,
web
is removed before db
and redis
.
Simple example:
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
Compose guarantees dependency services have been started before starting a dependent service. Compose waits for dependency services to be "ready" before starting a dependent service.
The long form syntax enables the configuration of additional fields that can't be expressed in the short form.
restart
: When set to true
Compose restarts this service after it updates the dependency service. This applies to an explicit restart controlled by a Compose operation, and excludes automated restart by the container runtime after the container dies.
condition
: Sets the condition under which dependency is considered satisfied
service_started
: An equivalent of the short syntax described above
service_healthy
: Specifies that a dependency is expected to be "healthy" (as indicated by healthcheck) before starting a dependent service.
service_completed_successfully
: Specifies that a dependency is expected to run to successful completion before starting a dependent service.
required
: When set to false
Compose only warns you when the dependency service isn't started or available. If it's not defined the default value of required
is true
.
Service dependencies cause the following behaviors:
- Compose creates services in dependency order. In the following example,
db
and redis
are created before web
.
- Compose waits for healthchecks to pass on dependencies marked with
service_healthy
. In the following example, db
is expected to be "healthy" before web
is created.
- Compose removes services in dependency order. In the following example,
web
is removed before db
and redis
.
services:
web:
build: .
depends_on:
db:
condition: service_healthy
restart: true
redis:
condition: service_started
redis:
image: redis
db:
image: postgres
Compose guarantees dependency services are started before starting a dependent service. Compose guarantees dependency services marked with
service_healthy
are "healthy" before starting a dependent service.