Dockerfile HEALTHCHECK: test application health
Read the recipe
FROM busybox:1.37RUN touch /readyHEALTHCHECK --interval=2s --timeout=1s --retries=2 CMD test -f /ready || exit 1CMD ["sleep", "120"]The probe checks a synthetic marker file, not an HTTP service. Exit 0 means success, 1 indicates failure; 2 is reserved. Health starts as starting, becomes healthy after success, and becomes unhealthy after the configured consecutive failures. The timeout limits each probe. A start period can give slow applications time to initialize. This is a bounded demonstration; a real probe should check useful application behavior without side effects. A startup grace period can exclude early failures from the retry count until success or expiry. This fixture has no explicit start-period. Capture the probe history as well as overall state; if the process exits, it is not evidence of a running unhealthy container.
Try it in a local lab
Manual lab only, not an interactive ByteMind execution. Use an approved disposable offline Linux VM, verified local Engine and Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Confirm the local context/builder, reviewed preloaded busybox:1.37 with root default, no entrypoint, and required applets. Record actual digest/platform and use reviewed digest for repeatability. No network egress; --network none is not a complete builder egress control. Verify every bm-v08 image tag and container name in the commands is unused. Use synthetic data only. Run commands individually and stop on unexpected failure; remove only resources created by this lab after ownership checks. If cleanup cannot be confirmed, retire the disposable VM. Expected results are predictions, not observed output. Before contacting any Engine, resolve DOCKER_HOST, DOCKER_CONTEXT and CLI context overrides; also check BUILDX_BUILDER and any builder override. Confirm that the actual builder used by docker build is the approved local docker driver. Review the base configuration for inherited triggers, volumes, health checks, user, shell and required applets; stop if assumptions differ. Make a private working copy containing only the supplied synthetic files. An approved digest may require registry metadata resolution even when layers are present locally; if the offline builder cannot resolve it, stop and record the limitation. Record the actual base image ID, platform, builder version and output image ID; never substitute an unknown digest. Do not retry by enabling egress. Apply owner-approved time and resource budgets and retire the VM if cleanup is uncertain.
Open lab-fixtures/dockerfile-configuration/05-healthcheck/ from the extracted archive. Save the complete recipe and any additional files below in that directory.
The container runs for at most the intended 120 seconds of sleep unless stopped earlier. Before changing the marker, verify the named container belongs to this lab. Run the follow-up commands immediately; if it already exited, skip exec and remove the stopped lab container. Proceed to marker removal only after observing running healthy. After the second wait, inspect the probe records before stopping; if unhealthy has not appeared, record the test as inconclusive rather than extending the process.
docker build --load --network none -t bm-v08-healthcheck:lab .docker run -d --pull=never --network none --name bm-v08-health bm-v08-healthcheck:labdocker container inspect --format '{{.State.Status}} {{.State.Health.Status}}' bm-v08-healthsleep 5docker container inspect --format '{{.State.Status}} {{.State.Health.Status}}' bm-v08-health# Continue only after observing running healthy.docker exec bm-v08-health rm /readysleep 7docker container inspect --format '{{.State.Status}} {{.State.Health.Status}}' bm-v08-healthdocker container inspect --format '{{json .State.Health.Log}}' bm-v08-health# Record evidence and confirm ownership before cleanup.docker stop --time 2 bm-v08-healthdocker rm bm-v08-healthInitially expect running and starting or healthy, depending on timing. After a successful probe expect running healthy; after removing /ready and two failed probes expect running unhealthy. Delays are illustrative; if scheduling delays change the result, inspect once more while the bounded process runs. A standalone unhealthy container is not automatically restarted just because health failed. If it exits early, remove only the named lab container after confirming ownership.
Confirm the tag belongs to this lab and no container depends on it. Remove it without forcing deletion. Build cache may remain until VM disposal. Record output and exit status before removing the exact lab tag. Recheck the tag against the recorded output image ID; a tag may have been reassigned.
Common mistake
Do not confuse a successful probe with proof that every request works. Do not configure a probe to perform destructive writes or expensive work. HEALTHCHECK NONE disables an inherited probe; use it only with a justified replacement strategy.
Manual practice
Explain why the process can remain running while the health status is unhealthy. Design a read-only check for an application that must load a local configuration before serving requests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dockerfile HEALTHCHECK: test application health — Knowledge check
Apply the configuration concept to a new image or container scenario.
What does probe exit code 0 indicate?
With the container still running, no start period, and two consecutive failed probes after a successful probe, what health status is expected in this recipe?
Does standalone Docker restart a container solely for unhealthy status?