BM: Baca dalam Bahasa Melayu
World D4: Set Meals
Wait until healthy: depends_on
You will learn: to make one dish wait until another is really ready.
Checked with Docker Engine 29 on 31 Oct 2026
The problem
Tap each point on the timeline.
Morning rush. The API starts too early. It calls a database that is still waking up. Tap each moment.
Watch
web:
depends_on: [api]
api:
depends_on: [db]First to start at the top. Put these in order. Use the arrows, or drag.
- web
- db
- api
depends_on: Who waits for whom. With condition: service_healthy, a dish waits until the other passes its health check. sets the start order. A dish starts after the dishes it depends on. Put the start order right.
Watch
api:
depends_on:
db:
condition: service_healthy
db:
image: postgres:18-alpine
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 2s
retries: 10Your laptop~/kedai-kancil
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- kedai-kancil-api:latest6 layers · 244MB
- kedai-kancil-web:latest9 layers · 92.6MB
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
- postgres:18-alpine9 layers · 433MB
- redis:8-alpine7 layers · 162MB
0 containers (0 running), 6 images, 0 lockers.
Started is not ready. A health check is a taste test. pg_isready asks db: ready yet? service_healthy makes api wait for a yes.
Try it
Right now api waits only until db has started. Make it wait until db is healthy. Then order the set meal.
Build a set meal: add the services, then join their phone lines and lockers.
Menu (tap or drag)
Your set meal
- web
build: ./web - api
build: ./api - db
postgres:18-alpine
compose.yaml 27 lines
services: web: build: ./web ports: - "8080:80" depends_on: api: condition: service_started api: build: ./api environment: DB_HOST: db depends_on: db: condition: service_started db: image: postgres:18-alpine environment: POSTGRES_PASSWORD: example volumes: - db-data:/var/lib/postgresql healthcheck: test: ["CMD", "pg_isready", "-U", "postgres"] interval: 2s retries: 10 volumes: db-data: {}
Hint
Open api’s “Waits for” list. Pick healthy for db.Predict
Someone deleted db’s password. Postgres exits at once. What does docker compose up -d do now?
Pick one answer.
Check
Run it and read the red line. Then ask db’s logs why.
Your laptop~/kedai-kancil
- No open hatches
Lunchboxes (containers)
- No containers yet
Frozen packs (images)
- kedai-kancil-api:latest6 layers · 244MB
- kedai-kancil-web:latest9 layers · 92.6MB
- nginx:1.30-alpine8 layers · 93.5MB
- node:24-alpine4 layers · 244MB
- postgres:18-alpine9 layers · 433MB
- redis:8-alpine7 layers · 162MB
0 containers (0 running), 6 images, 0 lockers.
Real words: dependency failed to start. Plain words: db stopped, so the dishes waiting for it never started.
You did it!
You can now make a dish wait until another is healthy.
Go deeper (optional)
Three conditions: service_started (the short list form), service_healthy, and service_completed_successfully for a one-off job, like making tables. Add restart: true and Compose also restarts api after it restarts db. docker compose up -d --wait returns only when every dish is running or healthy. CI uses it.
Real mission
docker compose up -ddocker compose psdocker compose up -d --wait
0 of 3 steps done · Your ticks are saved.
Bahasa Melayu
Waktu sibuk pagi. API hidup terlalu awal. Ia panggil pangkalan data yang masih baru bangun. Ketik setiap saat.
web:
depends_on: [api]
api:
depends_on: [db]depends_on (bergantung pada) tetapkan urutan mula. Satu hidangan hidup selepas hidangan yang ia perlukan. Susun urutan mula dengan betul.
api:
depends_on:
db:
condition: service_healthy
db:
image: postgres:18-alpine
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 2s
retries: 10Hidup belum tentu sedia. Health check ialah ujian rasa. pg_isready tanya db: dah sedia? service_healthy buat api tunggu jawapan ya.
Sekarang api tunggu sampai db hidup sahaja. Buat ia tunggu sampai db sihat. Kemudian pesan set hidangan.
Seseorang padam kata laluan db. Postgres terus keluar. Apa docker compose up -d buat sekarang?
Jalankan dan baca baris merah. Kemudian tanya log db kenapa.
Kata sebenar: dependency failed to start. Maksudnya: db berhenti, jadi hidangan yang menunggunya tak pernah hidup.
Kini anda boleh buat satu hidangan tunggu sampai yang lain sihat.
You finished this before. Practice is always welcome!
Official docs: Control startup order · Compose file reference: depends_on · Compose file reference: healthcheck
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?