Add HEALTHCHECK to Dockerfiles for Reliable Services
Add HEALTHCHECK to a Dockerfile, observe container health states, and gate Compose service startup with depends_on condition service_healthy.

Lab overview
A running container is not the same thing as a ready service. Docker considers a container "running" the instant its main process starts, but that process may still be parsing config, warming caches, or waiting on a downstream port long after the container shows up in docker ps. When another service depends on it, that gap is where multi-container stacks break: the web tier connects before the database accepts queries, the worker dequeues before the broker is initialized, and the whole stack fails on first request.
The HEALTHCHECK Dockerfile directive closes that gap by attaching a probe to the container itself. Docker runs the probe on an interval, tracks consecutive failures, and exposes one of three states (starting, healthy, unhealthy) on the container. Compose then uses those states to sequence startup with depends_on: condition: service_healthy, so downstream services wait for actual readiness instead of mere process launch. In this lab, you will author a Dockerfile with a tuned HEALTHCHECK probe, watch the container transition between health states in real time, break the service intentionally to trigger an unhealthy verdict, and wire the same healthcheck into a Compose file that starts a Redis sidecar only after the web tier is genuinely serving traffic.
Objectives
Upon completion of this beginner level lab, you will be able to:
- Author a Dockerfile that adds a HEALTHCHECK directive with tuned
--interval,--timeout,--start-period, and--retriesflags. - Build and run the image, then observe the container transition from
startingtohealthyindocker ps. - Inspect probe history via
docker inspectto read the raw health log and exit codes. - Break the service inside a running container and watch the health state flip to
unhealthyonce the retry budget is exhausted. - Translate the healthcheck into a Compose file and gate a downstream Redis service with
depends_on: condition: service_healthy.
Who is this lab for?
This lab is designed for:
- Beginner Docker users who can build and run a basic image but have not yet worked with container health probes.
- Developers and SREs adopting Compose for local multi-service stacks.
- Anyone tired of debugging "container is up, service is not" startup races.
Verified against your live environment
An automated validation engine inspects your actual resources and configurations as you work. Completion means the task was performed, not multiple choice, real-world proficiency.
More labs like this
Running Containers with docker run and kubectl
Run nginx:alpine first with docker run then with kubectl run on the same VM, and see how Kubernetes maps onto Docker concepts.
Deploy Multi-Container Apps with Docker Compose
Author a compose.yaml that runs nginx, a Python API, and Postgres as a single declarative multi-service stack.
Speed Up Docker Builds with Layer Caching and dockerignore
Refactor a slow Dockerfile to leverage layer caching, add a .dockerignore to shrink the build context, and inspect the resulting image with docker history and docker diff.
Related reading
Environment
Every lab includes
- Real environment, pre-credentialed
- Automated checks on every step
- Isolated sandbox, auto cleanup
- AI-recommended next steps
Lab curriculum
- 01
Getting Started with Docker IDE
- 02
Author a Dockerfile With a HEALTHCHECK Directive
1 automated check
- 03
Run the Container and Observe Health States
1 automated check
- 04
Break the Service to Trigger an Unhealthy Verdict
1 automated check
- 05
Gate a Compose Service With service_healthy
1 automated check
Not the lab you were looking for?
Browse 200+ hands-on labs across AWS, Azure, Kubernetes, Docker, and cloud security.