Service Dependencies (depends_on) & Health-Check Conditions (service_healthy)
Order startup with depends_on and wait for health before dependents start.
In plain words
Use a narrow healthcheck for prerequisite services, then make the app retry or fail visibly if a dependency becomes unavailable later.
Think of it like…
depends_on is the recipe saying “preheat the oven first”; the app still needs to handle the oven being switched off during cooking.
Where you’ve seen it
In practice, depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 } is the concrete move for this job: order startup with depends_on and wait for health before dependents start. The release is not accepted until docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp confirms the result.
Words you’ll hear
- depends_on
- The implementation control in
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }for order startup with depends_on and wait for health before dependents start. - service_healthy
- The result to observe with
docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp, rather than infer from a successful command alone. - Startup Order
- The production boundary exposed when launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness.
Visual diagram
Theory & deep dive
depends_on control path
The concrete mechanism is depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }. The observable result is docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp; if those disagree, inspect the runtime evidence before changing unrelated settings.
Order startup with depends_on and wait for health before dependents start. Keep depends_on, service_healthy and Startup Order in the same deployment record so the intent and the observed state cannot drift apart.
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }
docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp, not with command success alone.Startup Order failure boundary
Many teams mistake startup order for recovery design. Docker Compose can wait during initial start but cannot continuously guarantee dependency health, and a failed health command can lock dependents behind a cycle forever. The narrow failure to design for is launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness.
Accept this compromise explicitly: Startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model. The controlled check-docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp-also identifies the point where a different design is safer.
Exercise this failure safely: launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Many teams mistake startup order for recovery design. Docker Compose can wait during initial start but cannot continuously guarantee dependency health, and a failed health command can lock dependents behind a cycle forever.
- Intent
- depends_on
When to use it & trade-offs
What it is
depends_on short form orders creation and stop, not full application readiness. The long form with condition: service_healthy waits for Docker health status; a database process can still be up while its schema, credential or migration is not ready.
Why it matters: Many teams mistake startup order for recovery design. Docker Compose can wait during initial start but cannot continuously guarantee dependency health, and a failed health command can lock dependents behind a cycle forever.
When to reach for it
- The fit is strongest for this job: order startup with depends_on and wait for health before dependents start. Pair it with
docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestampin the runbook. - Production depends_on changes should make service_healthy observable through
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }, rather than trusting the runtime default. - Keep a rollback example for the case where launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness.
When to avoid or be careful
- Do not accept command success while the system can still fail by launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness.
- Avoid erasing Startup Order protection merely to dodge this cost: “Startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model”.
- Do not replace the evidence path-
docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp-with a screenshot or a mutable tag.
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }: practical choices
Strengths: depends_on short form orders creation and stop, not full application readiness. The long form with condition: service_healthy waits for Docker health status; a database process can still be up while its schema, credential or migration is not ready.
Watch out: Startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model.
Strengths: It can begin with depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 } and fewer surrounding controls.
Watch out: It silently accepts launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness, which makes the result hard to reproduce after promotion.
Strengths: Many teams mistake startup order for recovery design. Docker Compose can wait during initial start but cannot continuously guarantee dependency health, and a failed health command can lock dependents behind a cycle forever.
Watch out: The platform adds an ownership and debugging layer; retain docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp as the direct check.
Interview signals & numbers to know
When this topic comes up
- depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }
- docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp
- Failure signature: launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness
- Trade-off: Startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model.
Benchmarks & rules of thumb
- 5 seconds
- example health interval
- 12 retries
- maximum initial health attempts in the example
- 90 seconds
- overall Compose wait budget
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }as proof and skippingdocker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp. - Launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness
- Optimizing depends_on without accepting that startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model.
Likely interviewer follow-ups
- Why does
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }address depends_on here rather than through application code? - How would you distinguish healthy service_healthy from the failure “launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness” using
docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp? - When would this trade-off-“Startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model”-justify a different Startup Order design?
depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }-and one proof-docker compose up --wait --wait-timeout 90 and inspect db health plus the dependent service start timestamp. Then name the boundary: launching the API only once the port is open without retry logic, or relying on a healthcheck that calls a remote dependency as if it were local liveness.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Service Dependencies (depends_on) & Health-Check Conditions (service_healthy) with generative AI interview Q&A and hands-on practice.