Beginner LevelDocker & Deployment
How does the HEALTHCHECK instruction monitor container health in production?
❓ Interview Prompt: Explain Docker HEALTHCHECK configuration, healthy/unhealthy status transitions, and integration with Docker Compose depends_on.
The HEALTHCHECK instruction tells Docker how to periodically test if the container's application is truly functioning — not just that the process is running.
Container Health States:
•
starting: Initial grace period — health checks are running but not yet evaluated.
•
healthy: Health check command returned exit code
0.•
unhealthy: Health check failed
retries consecutive times (exit code non-zero).HEALTHCHECK Parameters:
•
--interval: How often to run the check (default: 30s).•
--timeout: How long to wait for the check command (default: 30s).•
--start-period: Grace period for app startup before checks count (default: 0s).•
--retries: Consecutive failures before marking unhealthy (default: 3).Why It Matters:
•
Docker Compose
depends_on: condition: service_healthy waits for the health check.•
Kubernetes readiness probes have a similar concept.
•
Swarm/orchestrators restart unhealthy containers automatically.
Code Implementation Example
dockerfile
FROM node:20-alpine
WORKDIR /app
COPY . .
RUN npm ci
# HEALTHCHECK — curl the /health endpoint every 30s
HEALTHCHECK \
--interval=30s \
--timeout=5s \
--start-period=10s \
--retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# For Alpine without curl, use wget:
# CMD wget -qO- http://localhost:3000/health || exit 1
EXPOSE 3000
CMD ["node", "server.js"]💡HEALTHCHECK pings /health every 30s — after 3 consecutive failures the container is marked unhealthy.
🎯 Interview Key Takeaway
HEALTHCHECK transitions containers between healthy/unhealthy states based on periodic command checks — enabling orchestrators to restart truly broken containers.
#Docker#HEALTHCHECK#Health Monitoring#Docker Compose#Beginner
Back to all Docker & Deployment Questions→