Production — Cheat Sheet
Docker · 1 topics. Download the PDF or the Instagram carousel and share it.
Cheat Sheet · AiCanCode.org
Production
Docker1 topicsQuick revision reference
1
Production Patterns — Health Checks, Restart Policies & Logging
Running containers in production requires health checks, restart policies, log management, and resource limits to ensure reliability, observability, and efficient resource use.
- ✓HEALTHCHECK defines how Docker probes application health (not just process alive).
- ✓Unhealthy containers are automatically restarted based on the restart policy.
- ✓Handle SIGTERM in your application for graceful shutdown — drain in-flight requests before exiting.
- ✓Default json-file log driver loses logs when a container is removed — use centralised logging in production.
- ✓Set max-size and max-file on the json-file driver to prevent disk exhaustion.
- ✓The start-period in HEALTHCHECK prevents premature unhealthy marking during slow startup (e.g., JVM warmup).
Dockerfile + Compose — health checks
# Dockerfile HEALTHCHECK
HEALTHCHECK --interval=30s \ # check every 30s
--timeout=5s \ # mark unhealthy if check takes >5s
--start-period=10s \ # grace period after container starts
--retries=3 \ # 3 consecutive failures = unhealthy
CMD curl -f http://localhost:8000/health || exit 1
# Or use wget (smaller than curl in Alpine)
HEALTHCHECK CMD wget -qO- http://localhost:8000/health || exit 1
# Check health status
docker ps
# STATUS
# Up 5 minutes (healthy)
# Up 2 minutes (unhealthy)
# Up 10 seconds (starting) ↠during start-period
# Inspect health history
docker inspect --format='{{json .State.Health}}' my-container | jq
# In docker-compose.yml (inline healthcheck):
services:
api:
image: my-api
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40sLearn this free with Aria, your AI tutor → AiCanCode.org/learn/docker