Docker Health Checks & Probes
Complete guide to Docker HEALTHCHECK instructions, health status monitoring, interval, timeout, retries, and integration with Docker Compose and orchestration.
By default, Docker only checks if a container's main process (PID 1) is running. A container may be deadlocked or stuck in an infinite loop while still reporting a running status. Health checks periodically probe the application inside the container to verify it is actually serving traffic.
The HEALTHCHECK Dockerfile Instruction
HEALTHCHECK [OPTIONS] CMD commandSupported Options
--interval=DURATION(default:30s): How often the probe runs.--timeout=DURATION(default:30s): Maximum time allowed for the check before failing.--start-period=DURATION(default:0s): Grace period for container bootstrap before failures count towards retries.--retries=N(default:3): Number of consecutive failures needed to mark containerunhealthy.
Practical Examples
1. HTTP Endpoint Health Check (Node.js / Express / Next.js)
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/api/health || exit 12. Native Node.js Healthcheck Script (No curl/wget required)
// healthcheck.js
const http = require('http');
const req = http.request('http://localhost:3000/api/health', (res) => {
process.exit(res.statusCode === 200 ? 0 : 1);
});
req.on('error', () => process.exit(1));
req.end();HEALTHCHECK --interval=15s --timeout=3s CMD ["node", "healthcheck.js"]Health Checks in Docker Compose
services:
database:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: secret
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
interval: 5s
timeout: 3s
retries: 5
api:
build: .
depends_on:
database:
condition: service_healthyUsing condition: service_healthy ensures dependent services wait until the database is fully initialized and accepting socket connections before starting!
Inspecting Health Status
# View container status with health status
docker ps
# Inspect detailed health check logs and exit codes
docker inspect --format='{{json .State.Health}}' my-container | jq .Related Topics
The .dockerignore File & Build Optimization
Complete guide to using .dockerignore to exclude unnecessary files from the build context, speed up builds, and protect secrets.
Docker Resource Management & Limits
Complete guide to limiting container CPU, memory, swap, and disk I/O using Linux control groups (cgroups v2) to prevent noisy neighbor and OOM kills.