पाठ 12 / 25
Health Checks
Tell Compose when a service is ready.
test, interval, timeout, retries
A healthcheck runs a command inside the container at an interval; if it fails retries times in a row the container is marked unhealthy. start_period gives slow services time before failures count. Use a cheap command that proves readiness: pg_isready for PostgreSQL, an HTTP request to a health endpoint for APIs (the tool you call, such as wget or curl, must exist in the image). docker compose ps shows health status.
The database health check, run
I ran this with Docker Compose v2.38.1 and jq in the demo "shop" project. docker compose config parses, interpolates, merges and validates the files without starting containers; the Docker daemon was not running, so nothing was started. The db health check uses pg_isready every 5 seconds with 10 retries; CMD-SHELL runs it through a shell.
docker compose config --format json | jq -c ".services.db.healthcheck"
Output:
{"test":["CMD-SHELL","pg_isready -U shop -d shop"],"interval":"5s","retries":10}Check the tool exists in the image
A health check calling curl in an image without curl fails forever; use what the image provides or add it.
त्वरित जाँच: What does a health check enable in depends_on?
- Automatic scaling
- The service_healthy condition, so dependents wait for readiness
- Image building
- Port publishing
Answer
The service_healthy condition, so dependents wait for readiness — Health checks define readiness.