# Reference: Docker Compose Resources Syntax Riferimento completo per la configurazione delle risorse in Docker Compose. ## Sezione deploy.resources ### Struttura Completa ```yaml services: service_name: deploy: resources: limits: cpus: 'VALUE' memory: 'VALUE' reservations: cpus: 'VALUE' memory: 'VALUE' ``` ## Parametri Limits ### cpus **Formato:** Stringa con numero decimale ```yaml cpus: '0.5' # 50% di 1 CPU cpus: '1' # 1 CPU completa cpus: '2' # 2 CPU complete cpus: '1.5' # 1.5 CPU cpus: '0.25' # 25% di 1 CPU ``` **Validità:** - Minimo: `0.001` (1 millesimo di CPU) - Massimo: Numero di CPU host (es. 8 su host con 8 core) - Default: Nessun limite (tutte le CPU disponibili) ### memory **Formato:** Stringa con unità ```yaml memory: '512M' # 512 Megabyte memory: '1G' # 1 Gigabyte memory: '128M' # 128 Megabyte memory: '4G' # 4 Gigabyte memory: '1024M' # 1024 Megabyte (equivale a 1G) ``` **Unità Supportate:** - `B` - Byte - `K` / `KB` - Kilobyte (1024 bytes) - `M` / `MB` - Megabyte (1024 KB) - `G` / `GB` - Gigabyte (1024 MB) **Validità:** - Minimo: `4M` (4 Megabyte) - Massico: Memoria host disponibile - Default: Nessun limite (tutta la memoria disponibile) ## Parametri Reservations ### Scopo Le **reservations** garantiscono risorse minime disponibili: - Docker riserva queste risorse per il container - Usato per pianificazione capacity (Kubernetes) - Meno comune in Docker Compose locale ### Esempio ```yaml deploy: resources: limits: cpus: '2' # Massimo 2 CPU memory: '4G' # Massimo 4 GB reservations: cpus: '1' # Garantite 1 CPU memory: '2G' # Garantiti 2 GB ``` **Uso tipico:** - `limits` - Definisce il tetto massimo (enforcement) - `reservations` - Definisce il pavimento minimo (garanzia) ## Esempi Completi per Servizi ### Web Server (t2.micro) ```yaml web: image: nginx:alpine deploy: resources: limits: cpus: '1' memory: 1G ``` ### Application Server (t2.small) ```yaml app: image: node:alpine deploy: resources: limits: cpus: '1' memory: 2G ``` ### Database (t2.medium) ```yaml db: image: postgres:16-alpine deploy: resources: limits: cpus: '2' memory: 4G reservations: cpus: '1' memory: 2G ``` ### Worker CPU-Intensive (c5.xlarge) ```yaml worker: image: python:alpine deploy: resources: limits: cpus: '4' memory: 4G ``` ## Validation ### Verifica con docker compose config ```bash docker compose config ``` Output per servizio con limits: ```yaml services: web: deploy: resources: limits: cpus: '1' memory: 1073741824 # 1G in byte ``` ### Verifica con docker inspect ```bash docker inspect lab03-web --format '{{.HostConfig}}' ``` Output: ``` map[NanoCpus:1000000000 Memory:1073741824] ``` Conversione: - `NanoCpus: 1000000000` = 1 CPU (1e9 nanocpus) - `Memory: 1073741824` = 1 GB (in byte) ## Troubleshooting ### Errore: "no matching resources" **Causa:** Host non ha abbastanza risorse **Soluzione:** ```yaml # Riduci i limits deploy: resources: limits: cpus: '0.5' # Era '2' memory: 512M # Era '2G' ``` ### Errore: "invalid memory format" **Causa:** Formato non valido **Soluzione:** ```yaml # ✗ WRONG memory: 1024 # Manca unità memory: "1GB" # "GB" non valido (usa G) # ✓ CORRECT memory: 1G memory: 1024M ``` ## Vedi Anche - Tutorial: Configurare Limiti delle Risorse - Reference: EC2 Instance Mapping