# How-to: Verificare l'Utilizzo delle Risorse Come monitorare l'utilizzo CPU e memoria dei container Docker. ## Utilizzo Base ### Snapshot Singolo ```bash docker stats --no-stream ``` Output: ``` CONTAINER NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 12345 lab03-web 0.01% 2.5MiB / 1GiB 0.24% 1.2kB / 0B 0B / 0B 2 ``` ### Monitoraggio in Tempo Reale ```bash docker stats ``` Premi `Ctrl+C` per uscire. ### Container Specifico ```bash docker stats lab03-web ``` ## Formattazione Avanzata ### Solo Container e CPU/Memoria ```bash docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" ``` ### Output senza header ```bash docker stats --no-stream --format "{{.Container}}: {{.CPUPerc}}, {{.MemUsage}}" ``` ### Output CSV ```bash docker stats --no-stream --format "{{.Container}},{{.CPUPerc}},{{.MemUsage}}" ``` ## Interpretare l'Output ### CPU Percentage - `0.01%` - Container idle - `50%` - Container usa mezza CPU - `100%` - Container usa 1 CPU completa - `>100%` - Container usa più di 1 CPU (multi-core) ### Memory Usage - `2.5MiB / 1GiB` - Usati 2.5 MB su 1 GB di limite - `512MiB / 512MiB` - Al limite (potrebbe causare OOM) - `980MiB / 1GiB` - Vicino al limite (watch!) ### Memory Percentage - `<50%` - Sotto l'half del limite (OK) - `50-80%` - Nella norma (monitorare) - `>80%` - Vicino al limite (attenzione) - `>95%` - A rischio di OOM kill ## Troubleshooting ### Container usa 0% CPU Container potrebbe essere idle o bloccato. Verifica: ```bash docker exec lab03-web ps aux ``` ### Memory usage alto Identifica il processo che usa più memoria: ```bash docker exec lab03-web ps aux --sort=-%mem | head -5 ``` ### Container OOM killed Cerca "OOM" nei log: ```bash docker inspect lab03-web --format '{{.State.OOMKilled}}' ``` ## Vedi Anche - How-to: Testare Limits Enforcement - Reference: Compose Resources Syntax