Docker Compose - Snippets Réutilisables
Configurations Docker Compose standards à réutiliser dans vos services.
Healthcheck Standard
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
Variantes
Pour services sans endpoint /health:
healthcheck:
test: ["CMD", "pgrep", "-x", "python"] # Vérifie que le processus tourne
interval: 30s
timeout: 10s
retries: 3
Pour bases de données (MySQL):
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 5
Pour bases de données (PostgreSQL):
Pour Redis:
Rotation des Logs
Variantes selon taille attendue:
# Services légers
logging:
driver: json-file
options:
max-size: '5m'
max-file: '2'
# Services verbeux (transcription, processing)
logging:
driver: json-file
options:
max-size: '50m'
max-file: '5'
Restart Policy
Alternatives:
no: Ne jamais redémarrer automatiquementalways: Toujours redémarrer (même après reboot)on-failure: Redémarrer seulement si échecunless-stopped: Redémarrer sauf si arrêt manuel (recommandé)
Networks
Réseau externe (partagé entre services):
Volumes
volumes:
- app-data:/var/lib/app # Volume nommé
- ./config:/app/config:ro # Mount read-only
- /host/path:/container/path # Bind mount
# Déclaration en bas du fichier
volumes:
app-data:
Environment Variables
Depuis fichier .env:
Dépendances
Resource Limits
User & Permissions
Security
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
cap_add:
- NET_ADMIN # Si besoin réseau avancé
Exemple Complet
version: '3.8'
services:
app:
image: myapp:latest
container_name: myapp
restart: unless-stopped
ports:
- '8080:8080'
environment:
- NODE_ENV=production
- DATABASE_URL=postgresql://db:5432/mydb
volumes:
- app-data:/var/lib/app
- ./config:/app/config:ro
networks:
- app-network
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
logging:
driver: json-file
options:
max-size: '10m'
max-file: '3'
deploy:
resources:
limits:
cpus: '2.0'
memory: 4G
db:
image: postgres:15
container_name: myapp-db
restart: unless-stopped
environment:
- POSTGRES_DB=mydb
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
networks:
- app-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U myuser"]
interval: 10s
timeout: 5s
retries: 5
logging:
driver: json-file
options:
max-size: '10m'
max-file: '3'
redis:
image: redis:7-alpine
container_name: myapp-redis
restart: unless-stopped
networks:
- app-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
logging:
driver: json-file
options:
max-size: '5m'
max-file: '2'
volumes:
app-data:
db-data:
networks:
app-network:
driver: bridge
Voir aussi
- Infrastructure > Docker - Commandes et gestion Docker
- Guides > Docker Autostart - Configuration auto-start