Health checks in Compose in Docker - Time & Space Complexity
We want to understand how the time taken by Docker Compose health checks changes as the number of services grows.
Specifically, how does adding more containers with health checks affect the total checking time?
Analyze the time complexity of this Docker Compose health check setup.
version: '3.8'
services:
web:
image: nginx
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
db:
image: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 30s
timeout: 10s
retries: 5
This Compose file defines two services, each with its own health check that runs periodically.
Look for repeated health check commands and their frequency.
- Primary operation: Each service runs its health check command repeatedly at set intervals.
- How many times: Each health check runs independently and periodically, repeating indefinitely.
As the number of services with health checks increases, the total number of health check commands run per interval grows.
| Number of Services (n) | Health Check Commands per Interval |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The total health check commands grow directly with the number of services.
Time Complexity: O(n)
This means the total health check work grows linearly as you add more services.
[X] Wrong: "Health checks run once and do not add to ongoing work as services increase."
[OK] Correct: Health checks run repeatedly and independently for each service, so more services mean more repeated checks.
Understanding how repeated tasks scale helps you design systems that stay responsive and manageable as they grow.
What if all health checks ran sequentially instead of independently? How would the time complexity change?
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
