What if your containers could tell you when they're sick, all by themselves?
Why Container health checks in Docker? - Purpose & Use Cases
Imagine you run many containers for your app, and you have to check each one manually to see if it is working fine. You open logs, run commands inside containers, and guess if they are healthy.
This manual checking is slow and tiring. You might miss a broken container or restart one that is actually fine. It's easy to make mistakes and waste time fixing problems that could be caught early.
Container health checks let the system automatically test if each container is working well. If a container is unhealthy, it can be restarted or flagged without your constant attention. This keeps your app running smoothly without manual checks.
docker ps
# Then docker exec to check logs or statusHEALTHCHECK CMD curl -f http://localhost/ || exit 1Automatic monitoring and recovery of containers to keep applications reliable and reduce manual work.
A web app container uses a health check to ping its own server every minute. If the server stops responding, the container restarts automatically, avoiding downtime.
Manual container checks are slow and error-prone.
Health checks automate status monitoring inside containers.
This leads to faster recovery and more reliable apps.
Practice
HEALTHCHECK in a Docker container?Solution
Step 1: Understand the role of HEALTHCHECK
The HEALTHCHECK instruction runs a command inside the container to verify if the app is working properly.Step 2: Identify what HEALTHCHECK does not do
It does not restart Docker daemon, update images, or increase CPU usage.Final Answer:
To check if the container's application is running correctly -> Option CQuick Check:
HEALTHCHECK verifies app status = B [OK]
- Confusing HEALTHCHECK with Docker daemon restart
- Thinking HEALTHCHECK updates images
- Assuming HEALTHCHECK changes resource usage
Solution
Step 1: Recall Dockerfile HEALTHCHECK syntax
The correct syntax usesHEALTHCHECK CMDfollowed by the command to run.Step 2: Identify incorrect keywords
RUN, EXEC, CHECK are not valid keywords for HEALTHCHECK instruction.Final Answer:
HEALTHCHECK CMD curl -f http://localhost/ || exit 1 -> Option AQuick Check:
HEALTHCHECK uses CMD keyword = C [OK]
- Using RUN instead of CMD in HEALTHCHECK
- Using EXEC or CHECK which are invalid
- Omitting CMD keyword entirely
HEALTHCHECK CMD curl -f http://localhost/health || exit 1 CMD ["./start-server.sh"]
What will be the health status if
curl fails?Solution
Step 1: Understand HEALTHCHECK command behavior
If the command returns a non-zero exit code (like exit 1), Docker marks the container as unhealthy.Step 2: Analyze the curl failure effect
Sincecurl -ffails, it triggersexit 1, so health status becomes unhealthy.Final Answer:
unhealthy -> Option AQuick Check:
Failed HEALTHCHECK command = unhealthy [OK]
- Assuming failure means healthy
- Confusing 'starting' with unhealthy
- Thinking status stays unknown on failure
HEALTHCHECK CMD curl http://localhost/health || exit 1
But your container health status is always
starting. What is the likely problem?Solution
Step 1: Understand 'starting' health status meaning
'starting' means the container is still within the initial grace period before health checks are considered final.Step 2: Analyze why health check stays 'starting'
If the command never succeeds during this period, Docker keeps status as 'starting'. Syntax is correct, Docker daemon running, and network likely accessible.Final Answer:
The health check command never succeeds within the start period -> Option DQuick Check:
Failing health check in start period = starting [OK]
- Assuming syntax error causes 'starting' status
- Blaming Docker daemon when it runs fine
- Ignoring network issues but they are less likely here
Solution
Step 1: Identify correct flags for interval and retries
--interval sets wait time between checks; --retries sets how many failures before unhealthy.Step 2: Match values to requirements
We want 10 seconds between checks and 3 retries, so--interval=10s --retries=3is correct. Timeout and start-period are different settings.Final Answer:
HEALTHCHECK --interval=10s --retries=3 CMD curl -f http://localhost/health || exit 1 -> Option BQuick Check:
Interval=10s and retries=3 flags = A [OK]
- Confusing timeout with interval
- Swapping retries and interval values
- Using start-period instead of interval
Ready to prove it?
Pass the test and this pattern moves unknown
