What if your app could know exactly when its parts are ready, every time?
Why Health checks in Compose in Docker? - Purpose & Use Cases
Imagine you run multiple services using Docker Compose, and you want to know if each service is working properly before others start depending on it.
Without health checks, you have to guess or manually check if a service is ready.
Manually checking service status is slow and error-prone.
You might start dependent services too early, causing failures or crashes.
It's like waiting for a cake to bake by opening the oven repeatedly, losing heat and ruining the cake.
Health checks in Compose automatically test if a service is healthy.
Compose waits for the service to be ready before starting others that depend on it.
This makes your setup reliable and smooth without manual checks.
services:
web:
image: myapp
depends_on:
- dbservices:
db:
image: postgres
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 10s
retries: 5
web:
image: myapp
depends_on:
db:
condition: service_healthyYou can build dependable multi-service apps that start in the right order, avoiding errors and downtime.
When running a web app that needs a database, health checks ensure the database is ready before the web server starts accepting users.
Manual checks are slow and risky.
Health checks automate readiness detection.
Compose uses health status to manage service startup order.
Practice
Solution
Step 1: Understand health check purpose
Health checks run commands inside the container to confirm the app is working well.Step 2: Differentiate from other Docker tasks
Restarting, building, and exposing ports are separate Docker tasks, not health checks.Final Answer:
To verify if the container's application is running correctly -> Option BQuick Check:
Health check = verify app status [OK]
- Confusing health checks with container restart policies
- Thinking health checks build images
- Mixing health checks with port mapping
Solution
Step 1: Recall Docker Compose healthcheck syntax
The correct key is 'healthcheck' with a 'test' array specifying the command.Step 2: Validate command format
The 'test' must be an array starting with 'CMD' followed by the command and arguments.Final Answer:
healthcheck: { test: ["CMD", "curl", "-f", "http://localhost"] } -> Option CQuick Check:
Use 'healthcheck' with 'test' array [OK]
- Using wrong keys like 'health_check' or 'run'
- Passing command as a string instead of array
- Omitting 'CMD' in the test array
healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 10s retries: 3
Solution
Step 1: Understand retries and failure effect
The health check runs every 10 seconds and allows 3 retries before marking unhealthy.Step 2: Identify container behavior on failure
Failing the test 3 times marks the container unhealthy; it does not restart or stop automatically.Final Answer:
The container will be marked as unhealthy after 3 failed attempts -> Option AQuick Check:
Retries control unhealthy marking, not restart [OK]
- Assuming container restarts on health check failure
- Thinking container stops immediately on failure
- Ignoring the retries setting
healthcheck: test: curl -f http://localhost || exit 1 interval: 5s retries: 2What is the problem?
Solution
Step 1: Check healthcheck test syntax
Docker Compose requires the test command as an array starting with 'CMD' or 'CMD-SHELL'.Step 2: Identify why string command fails
Using a plain string without array causes Compose to ignore or fail the health check.Final Answer:
The test command must be an array starting with CMD -> Option AQuick Check:
Use array with CMD for test command [OK]
- Writing test as plain string instead of array
- Misplacing healthcheck under wrong keys
- Using invalid interval units
check.sh inside the container returns success within 5 seconds. Which Compose healthcheck configuration is best?Solution
Step 1: Understand timeout usage in healthcheck
The 'timeout' key sets max time for the test command to run; here 5 seconds is needed.Step 2: Choose correct test format and timing
Using 'CMD' with './check.sh' is valid; 'timeout' controls max duration; interval and retries set frequency and tolerance.Final Answer:
healthcheck: test: ["CMD", "./check.sh"] timeout: 5s interval: 10s retries: 3 -> Option DQuick Check:
Use 'timeout' key to limit test duration [OK]
- Using shell timeout inside test instead of 'timeout' key
- Confusing interval and timeout values
- Omitting retries leading to quick unhealthy status
Ready to prove it?
Pass the test and this pattern moves unknown
