Add health checks to Docker containers
✓Works with OpenClaudeYou are a Docker operations engineer. The user wants to add health checks to Docker containers to monitor application status and enable automatic restart on failure.
What to check first
- Run
docker --versionto confirm Docker is installed - Verify your application has an endpoint or script that can determine health status (HTTP endpoint, script exit code, etc.)
Steps
- Identify your health check mechanism—either an HTTP endpoint (e.g.,
http://localhost:8080/health), a shell command (e.g.,curl localhost:8080/health), or a custom script that returns exit code 0 for healthy - In your Dockerfile, add a
HEALTHCHECKinstruction with--interval(check frequency),--timeout(max time to wait), and--retries(failures before unhealthy) - Set the
CMDparameter to the actual command—wrap HTTP checks withcurlorwget, or use your custom script - Build the image with
docker build -t myapp:latest . - When running the container with
docker run, Docker will automatically start health checks after the container begins - Monitor health status with
docker ps(shows(healthy),(unhealthy), or(starting)next to the container name) - Check detailed health history with
docker inspect --format='{{.State.Health}}' <container_id> - For compose stacks, add
healthcheckblock under each service withtest,interval,timeout, andretrieskeys
Code
# Dockerfile with HEALTHCHECK
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
# Install curl for health check command
RUN apk add --no-cache curl
# Define health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
# docker-compose.yml with healthcheck
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 5s
restart: unless-stopped
db:
image: postgres:15
environment:
POSTGRES_DB: myapp
POSTGRES_PASSWORD: secret
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Docker & Kubernetes Skills
Other Claude Code skills in the same category — free to download.
Dockerfile Generator
Generate optimized Dockerfile for any project
Docker Compose
Create docker-compose.yml for multi-service apps
K8s Deployment
Generate Kubernetes deployment manifests
K8s Service
Create Kubernetes service and ingress configs
Helm Chart
Create Helm chart for application
Docker Multistage
Optimize Docker builds with multi-stage builds
K8s ConfigMap
Create ConfigMaps and Secrets management
Docker Security
Audit and fix Dockerfile security issues
Want a Docker & Kubernetes skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.