Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 84/ 160
05. Docker Compose & Multi-Container Apps

Service Dependencies (depends_on) & Health-Check Conditions (service_healthy)

Order startup with depends_on and wait for health before dependents start.

Intermediatedepends_on · service_healthy3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Concrete depends_on setup

depends_on: { db: { condition: service_healthy } }; healthcheck: { test: [CMD-SHELL, pg_isready -U app -d shop], interval: 5s, timeout: 2s, retries: 12 }

Key takeaway: Prove service_healthy with 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.

Deliberate failure test

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.

Key takeaway: Startup conditions reduce race failures; idempotent retries and circuit breaking provide the stronger runtime recovery model.

How it works, step by step

Step 1 of 4
Design

1. 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.

Use a narrow healthcheck for prerequisite services, then make the app retry or fail visibly if a dependency becomes unavailable later.
Key numbers at this step
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 timestamp in 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

Explicit depends_on
Best for: Order startup with depends_on and wait for health before dependents start, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived service_healthy experiments with low blast radius.

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.

Managed Startup Order
Best for: Teams that centralize Startup Order policy across many workloads.

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 skipping docker 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Defining Services, Builds, Images & Container Names
Next topic
Networking in Compose (Default Networks, Custom Networks, Aliases)