docker HEALTHCHECK — add container health check
Quick Answer
# Dockerfile
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
Usage
You want Docker or Docker Compose to detect when a running container is no longer serving traffic correctly. Docker reports the status but does not restart an unhealthy container automatically. Kubernetes uses its own liveness and readiness probes instead.
Other causes & fixes
HEALTHCHECK options explained
interval controls how often the check runs, timeout limits each attempt, start-period provides a startup grace period, and retries sets the number of consecutive failures before the status becomes unhealthy.
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost/health || exit 1
Check health status
docker inspect --format '{{.State.Health.Status}}' my-container
# healthy | unhealthy | starting
# Show last few health log entries
docker inspect --format '{{range .State.Health.Log}}{{.Output}}{{end}}' my-container
Docker Compose healthcheck
# docker-compose.yml
services:
app:
image: my-app
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 5s
retries: 3
start_period: 10s
Disable an inherited healthcheck
# In a child image or Compose override
HEALTHCHECK NONE
Related