# How-to: Scrivere Healthchecks Personalizzati Come creare healthchecks custom per diversi tipi di servizi. ## Pattern Comuni ### HTTP Healthcheck ```yaml healthcheck: test: ["CMD-SHELL", "curl -f http://localhost/health || exit 1"] interval: 15s timeout: 3s retries: 3 ``` ### TCP Port Check ```yaml healthcheck: test: ["CMD-SHELL", "nc -z localhost 8080 || exit 1"] interval: 10s timeout: 2s retries: 3 ``` ### File Existence Check ```yaml healthcheck: test: ["CMD-SHELL", "test -f /var/run/app/ready || exit 1"] interval: 5s timeout: 1s retries: 5 ``` ### Database Connection ```yaml healthcheck: test: ["CMD-SHELL", "mysqladmin ping -h localhost || exit 1"] interval: 10s timeout: 5s retries: 5 ``` ### Redis Check ```yaml healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s timeout: 3s retries: 3 ``` ### Python Script Check ```yaml healthcheck: test: ["CMD-SHELL", "python /app/healthcheck.py || exit 1"] interval: 30s timeout: 10s retries: 3 ``` ## Best Practices ### 1. Check Leggeri ```yaml # ✓ GOOD - pagina leggera test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"] # ✗ AVOID - pagina pesante test: ["CMD", "wget", "--spider", "-q", "http://localhost/big-data"] ``` ### 2. Timeout Appropriati ```yaml # ✓ GOOD - timeout proporzionato all'interval interval: 10s timeout: 3s # ✗ AVOID - timeout troppo lungo interval: 10s timeout: 30s # Riduce frequenza dei check ``` ### 3. Retries Adeguati ```yaml # ✓ GOOD - più retries per servizi lenti (database) healthcheck: retries: 5 # ✓ GOOD - meno retries per servizi veloci (web) healthcheck: retries: 3 ``` ## Debugging ### Testare manualmente ```bash docker exec lab03-web wget --spider -q http://localhost/health echo $? # 0 = success, !0 = failure ``` ### Vedere log healthcheck ```bash docker inspect lab03-web --format '{{range .State.Health.Log}}{{.Output}} {{end}}' ``` ### Reset health status ```bash docker restart lab03-web ``` ## Vedi Anche - Tutorial: Implementare Healthchecks - Reference: Healthcheck Syntax