Docker · Containers in practice
What are healthchecks and how do you use them?
Short answer
A healthcheck is a command the runtime runs periodically to decide whether a container is healthy. It improves startup ordering and visibility but does not restart an unhealthy container by itself.
Docker records healthy, unhealthy and starting states based on the check. Compose can wait for service_healthy before starting dependents. The check should test the service's ability to do its job, not merely whether the process exists.
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
CMD wget -qO- http://127.0.0.1:3000/healthz || exit 1Design considerations
- Keep the check cheap; it runs for the life of the container
- Set a start period for applications with slow initialization
- Use timeouts shorter than the interval so checks do not pile up
- Distinguish liveness from readiness when the platform supports both
- Do not make a health endpoint depend on a non-critical third party
In Kubernetes, liveness and readiness probes have different consequences: a failed liveness probe restarts the container, while a failed readiness probe removes it from service endpoints. Docker's healthcheck is closer to a status signal, and restart policies handle recovery.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Does Docker restart a container when its healthcheck fails?
- 2What is the difference between liveness and readiness?
- 3Why is start-period useful?
- 4What should a health endpoint actually check?
Say it in the interview
A healthcheck is a periodic command that reports whether the container can serve traffic. I use a cheap endpoint with a start period and a timeout shorter than the interval, and I make it check the dependencies that matter. Docker reports the state but does not restart on failure, so I rely on Compose health conditions for ordering and on the orchestrator for probes in production.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.