Adding Container Health Checks (HEALTHCHECK Instruction)
Define HEALTHCHECK so orchestrators know when a container is actually ready.
In plain words
Write a cheap local indicator of the process’s ability to serve, then tune start period, interval, timeout and retries around real startup.
Think of it like…
A health check is a pulse oximeter: measure a small vital signal often, without turning every reading into a full medical exam.
Where you’ve seen it
In practice, HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1 is the concrete move for this job: define HEALTHCHECK so orchestrators know when a container is actually ready. The release is not accepted until docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown confirms the result.
Words you’ll hear
- HEALTHCHECK
- The implementation control in
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1for define HEALTHCHECK so orchestrators know when a container is actually ready. - Interval
- The result to observe with
docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown, rather than infer from a successful command alone. - Retries
- The production boundary exposed when expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness.
Visual diagram
Theory & deep dive
HEALTHCHECK control path
The concrete mechanism is HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1. The observable result is docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown; if those disagree, inspect the runtime evidence before changing unrelated settings.
Define HEALTHCHECK so orchestrators know when a container is actually ready. Keep HEALTHCHECK, Interval and Retries in the same deployment record so the intent and the observed state cannot drift apart.
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1
docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown, not with command success alone.Retries failure boundary
A health command that requires curl can fail in a distroless image; a deep probe that always queries the database can mark every replica unhealthy when one dependency is down. Docker itself does not restart a container based on unhealthy status. The narrow failure to design for is expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness.
Accept this compromise explicitly: Image-level health checks are useful standalone; orchestrators should own deployment readiness and may define independent probes. The controlled check-docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown-also identifies the point where a different design is safer.
Exercise this failure safely: expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A health command that requires curl can fail in a distroless image; a deep probe that always queries the database can mark every replica unhealthy when one dependency is down. Docker itself does not restart a container based on unhealthy status.
- Intent
- HEALTHCHECK
When to use it & trade-offs
What it is
HEALTHCHECK sets Docker to run a probe command periodically inside the container and records starting, healthy or unhealthy state. It is distinct from EXPOSE and from an orchestrator’s liveness/readiness probes, and the command must return 0 for success.
Why it matters: A health command that requires curl can fail in a distroless image; a deep probe that always queries the database can mark every replica unhealthy when one dependency is down. Docker itself does not restart a container based on unhealthy status.
When to reach for it
- The fit is strongest for this job: define HEALTHCHECK so orchestrators know when a container is actually ready. Pair it with
docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdownin the runbook. - Production HEALTHCHECK changes should make Interval observable through
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1, rather than trusting the runtime default. - Keep a rollback example for the case where expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness.
When to avoid or be careful
- Do not accept command success while the system can still fail by expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness.
- Avoid erasing Retries protection merely to dodge this cost: “Image-level health checks are useful standalone; orchestrators should own deployment readiness and may define independent probes”.
- Do not replace the evidence path-
docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown-with a screenshot or a mutable tag.
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1: practical choices
Strengths: HEALTHCHECK sets Docker to run a probe command periodically inside the container and records starting, healthy or unhealthy state. It is distinct from EXPOSE and from an orchestrator’s liveness/readiness probes, and the command must return 0 for success.
Watch out: Image-level health checks are useful standalone; orchestrators should own deployment readiness and may define independent probes.
Strengths: It can begin with HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1 and fewer surrounding controls.
Watch out: It silently accepts expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness, which makes the result hard to reproduce after promotion.
Strengths: A health command that requires curl can fail in a distroless image; a deep probe that always queries the database can mark every replica unhealthy when one dependency is down. Docker itself does not restart a container based on unhealthy status.
Watch out: The platform adds an ownership and debugging layer; retain docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown as the direct check.
Interview signals & numbers to know
When this topic comes up
- HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1
- docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown
- Failure signature: expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness
- Trade-off: Image-level health checks are useful standalone; orchestrators should own deployment readiness and may define independent probes.
Benchmarks & rules of thumb
- 15 seconds
- probe interval in the example
- 3 retries
- after the start window and before unhealthy
- 3 seconds
- per-probe timeout
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1as proof and skippingdocker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown. - Expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness
- Optimizing HEALTHCHECK without accepting that image-level health checks are useful standalone; orchestrators should own deployment readiness and may define independent probes.
Likely interviewer follow-ups
- Why does
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1address HEALTHCHECK here rather than through application code? - How would you distinguish healthy Interval from the failure “expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness” using
docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown? - When would this trade-off-“Image-level health checks are useful standalone; orchestrators should own deployment readiness and may define independent probes”-justify a different Retries design?
HEALTHCHECK --start-period=10s --interval=15s --timeout=3s --retries=3 CMD wget -q -T 2 -O- http://127.0.0.1:8080/health || exit 1-and one proof-docker inspect --format {{json .State.Health}} api and count consecutive probe failures after a controlled endpoint shutdown. Then name the boundary: expecting HEALTHCHECK to restart the service by itself, or making the endpoint call a remote database that is not part of local liveness.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Adding Container Health Checks (HEALTHCHECK Instruction) with generative AI interview Q&A and hands-on practice.