What is the HEALTHCHECK instruction in Dockerfile?
Answer
The HEALTHCHECK instruction tells Docker how to test whether the container is still working correctly. Docker runs the health check command periodically and updates the container's health status. Container health states: starting (initial period), healthy (check is passing), unhealthy (check has failed consecutively). Syntax: HEALTHCHECK [options] CMD command. Options: --interval=30s — how often to run (default 30s); --timeout=10s — how long to wait for response (default 30s); --retries=3 — number of consecutive failures to consider unhealthy (default 3); --start-period=40s — grace period before checking (for slow-starting apps). Example for a web server: HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=60s CMD curl -f http://localhost:3000/health || exit 1. Use exit 0 for healthy, exit 1 for unhealthy. View health status: docker ps shows health status; docker inspect --format "{{.State.Health.Status}}" container. In Docker Compose, depends_on can wait for a service to be healthy: depends_on: db: condition: service_healthy. In Docker Swarm, unhealthy tasks are automatically replaced. In Kubernetes, use liveness and readiness probes (separate, more powerful) instead.