Phase Plans (5 files): - 04-RESEARCH.md: Domain research on Docker limits, healthchecks, EC2 parallels - 04-VALIDATION.md: Success criteria and validation strategy - 04-01-PLAN.md: Test infrastructure (RED phase) - 04-02-PLAN.md: Diátxis documentation - 04-03-PLAN.md: Infrastructure implementation (GREEN phase) Test Scripts (6 files, 1300+ lines): - 01-resource-limits-test.sh: Validate INF-03 compliance - 02-healthcheck-test.sh: Validate healthcheck configuration - 03-enforcement-test.sh: Verify resource limits with docker stats - 04-verify-infrastructure.sh: Infrastructure verification - 99-final-verification.sh: End-to-end student verification - run-all-tests.sh: Test orchestration with fail-fast - quick-test.sh: Fast validation (<30s) Documentation (11 files, 2500+ lines): Tutorials (3): - 01-set-resource-limits.md: EC2 instance types, Docker limits syntax - 02-implement-healthchecks.md: ELB health check parallels - 03-dependencies-with-health.md: depends_on with service_healthy How-to Guides (4): - check-resource-usage.md: docker stats monitoring - test-limits-enforcement.md: Stress testing CPU/memory - custom-healthcheck.md: HTTP, TCP, database healthchecks - instance-type-mapping.md: Docker limits → EC2 mapping Reference (3): - compose-resources-syntax.md: Complete deploy.resources reference - healthcheck-syntax.md: All healthcheck parameters - ec2-instance-mapping.md: Instance type mapping table Explanation (1): - compute-ec2-parallels.md: Container=EC2, Limits=Instance Type, Healthcheck=ELB Infrastructure: - docker-compose.yml: 5 services (web, app, worker, db, stress-test) All services: INF-03 compliant (cpus + memory limits) All services: healthcheck configured EC2 parallels: t2.nano, t2.micro, t2.small, t2.medium, m5.large - Dockerfile: Alpine 3.19 + stress tools + non-root user Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3.6 KiB
3.6 KiB
Reference: Docker Compose Healthcheck Syntax
Riferimento completo per la configurazione degli healthchecks.
Sezione healthcheck
Struttura Completa
services:
service_name:
healthcheck:
test: ["CMD", "command", "arg1", "arg2"]
interval: 30s
timeout: 30s
retries: 3
start_period: 0s
Parametri
test (Required)
Comando da eseguire per verificare la salute.
Formato CMD:
test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"]
Formato CMD-SHELL:
test: ["CMD-SHELL", "curl -f http://localhost/ || exit 1"]
Formato NONE:
test: ["NONE"]
Disabilita healthcheck ereditato dall'immagine.
interval
Frequenza di esecuzione del test.
interval: 5s # Ogni 5 secondi
interval: 10s # Ogni 10 secondi
interval: 30s # Ogni 30 secondi (default)
interval: 1m # Ogni minuto
Validità:
- Minimo:
1s(1 secondo) - Massimo: Nessuno
- Default:
30s
timeout
Tempo massimo per completare il test.
timeout: 2s # 2 secondi
timeout: 5s # 5 secondi (default)
timeout: 30s # 30 secondi
Nota: Se il test supera il timeout, conta come fallimento.
retries
Numero di fallimenti consecutivi prima di marcare unhealthy.
retries: 1 # 1 fallimento = unhealthy
retries: 3 # 3 fallimenti = unhealthy (default)
retries: 5 # 5 fallimenti = unhealthy
Calcolo del tempo unhealthy:
tempo_totale = (interval + timeout) × retries
Esempio con interval: 10s, timeout: 5s, retries: 3:
- Check 1: 0s - 5s
- Check 2: 10s - 15s
- Check 3: 20s - 25s
- Unhealthy dopo: 25s
start_period
Grace period prima di contare retries verso unhealthy.
start_period: 0s # Nessun grace period (default)
start_period: 5s # 5 secondi di grace
start_period: 10s # 10 secondi di grace
start_period: 30s # 30 secondi di grace
Comportamento:
- Durante
start_period: Fallimenti non contano - Dopo
start_period:retriesinizi a contare
Esempi per Tipo di Servizio
HTTP Service (Nginx)
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/"]
interval: 10s
timeout: 5s
retries: 3
start_period: 5s
Database (PostgreSQL)
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
Cache (Redis)
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 3s
retries: 3
TCP Connection
healthcheck:
test: ["CMD-SHELL", "nc -z localhost 8080 || exit 1"]
interval: 15s
timeout: 2s
retries: 3
File Check
healthcheck:
test: ["CMD-SHELL", "test -f /tmp/ready || exit 1"]
interval: 5s
timeout: 1s
retries: 5
Stati di Salute
Ciclo di Vita
created → starting → healthy
↓
unhealthy
Transizioni:
| Da | A | Condizione |
|---|---|---|
| created | starting | Container avviato con healthcheck |
| starting | healthy | retries consecutivi passano |
| starting | unhealthy | retries consecutivi falliscono (dopo start_period) |
| healthy | unhealthy | retries consecutivi falliscono |
| unhealthy | healthy | 1 check passa |
Ispezionare Stato
# Stato corrente
docker inspect lab03-web --format '{{.State.Health.Status}}'
# Output: healthy / unhealthy / starting
# Exit code dell'ultimo check
docker inspect lab03-web --format '{{.State.Health.ExitCode}}'
# Output: 0 (success) o !0 (failure)
### Vedi Anche
- Tutorial: Implementare Healthchecks
- How-to: Custom Healthcheck