Sub topic 14
Optional dependencies with required: false
This chapter assumes you can write depends_on in long syntax, define a healthcheck, and use profiles. It also assumes a recent Compose v2 release. required was added in Compose 2.20.
The problem with hard dependencies
By default every depends_on entry is required. If the dependency can't be started, or it fails its condition, Compose stops and reports an error. That is right for a database your application can't run without.
Some dependencies are different. A cache, a metrics exporter, or a mail catcher may only be useful in some environments. If a service in a disabled profile is listed as a required dependency, Compose fails the project instead of skipping it. If an optional service is unhealthy, you may still want the main service to start.
Marking a dependency optional
Add required: false to a long-syntax entry. The default is required: true.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
cache:
image: redis:7
profiles: ["cache"]
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
web:
image: myapp:latest
depends_on:
db:
condition: service_healthy
cache:
condition: service_healthy
required: falseHere db stays a hard requirement. cache is optional, and it belongs to the cache profile, so a plain docker compose up -d does not start it.
What changes at runtime
The condition still decides ordering whenever the dependency is part of the project. The required flag only decides what happens when the dependency is absent or doesn't meet its condition.
| Situation | `required: true` (default) | `required: false` |
|---|---|---|
| Dependency is in a disabled profile | Compose reports an error | Compose skips it and starts the dependent |
| Dependency is enabled and becomes healthy | Dependent starts after it | Dependent starts after it |
| Dependency is enabled but unhealthy or failed | Compose stops with an error | Compose prints a warning and continues |
Try both modes:
docker compose up -d --wait
docker compose --profile cache up -d --waitThe first command starts db and web only. The second also starts cache, and web waits for it to become healthy.
Gotchas
- Optional means the application must cope. Compose will start
webwithoutcache. Ifwebcrashes when Redis is missing, you have moved the failure from Compose into your application. Use the retry and fallback logic from the previous chapter. - Don't use it to hide a broken dependency. A warning is easy to miss in
up -doutput. Keeprequired: truefor anything the service can't work without. - Older Compose versions reject the key. Check
docker compose versionif you see a validation error aboutrequired. - The condition still has to be valid.
service_healthyrequires a healthcheck on the dependency, even when the dependency is optional.
Exercise
Your web service lists mailcatcher (in the dev profile) under depends_on with condition: service_started. Running docker compose up without the dev profile fails. What is the smallest fix, and what must web still handle?
Show answer
Add required: false to the mailcatcher entry. Compose then skips the disabled service and starts web. The web application must handle the mail server being absent, for example by logging or ignoring failed SMTP connections.