Docker Compose Performance Optimization
Performance optimization is crucial for efficient containerized applications. This guide covers various strategies to improve Docker Compose performance.
Basic Performance Configuration
services:
app:
image: myapp:latest
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M
tmpfs:
- /tmp:rw,noexec,nosuid
ulimits:
nofile:
soft: 65535
hard: 65535
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
restart: unless-stopped
Docker Compose 3.x Features
- Extended Resource Management
services: app: deploy: resources: limits: cpus: ${CPU_LIMIT:-'0.50'} memory: ${MEMORY_LIMIT:-512M} reservations: cpus: ${CPU_RESERVATION:-'0.25'} memory: ${MEMORY_RESERVATION:-256M}
- Conditional Optimization
services: app: deploy: resources: limits: cpus: ${OPTIMIZE_CPU:-'0.50'} memory: ${OPTIMIZE_MEMORY:-512M} profiles: - optimized
- Performance Templates
services: app: deploy: resources: limits: cpus: ${CPU_${SERVICE_NAME:-app}_LIMIT:-'0.50'} memory: ${MEMORY_${SERVICE_NAME:-app}_LIMIT:-512M}
Performance Optimization Areas
- Resource Management
services: app: deploy: resources: limits: cpus: '0.50' memory: 512M reservations: cpus: '0.25' memory: 256M restart_policy: condition: on-failure max_attempts: 3
- Caching Strategies
services: app: volumes: - app_cache:/app/.cache:delegated - node_modules:/app/node_modules:delegated build: cache_from: - myapp:latest - myapp:${BUILD_CACHE_TAG:-latest}
- Container Tuning
services: app: ulimits: nofile: soft: 65535 hard: 65535 nproc: 65535 tmpfs: - /tmp:rw,noexec,nosuid,size=100m shm_size: 64m
Docker Swarm Mode Considerations
- Service Scaling
services: app: deploy: mode: replicated replicas: ${REPLICAS:-3} update_config: parallelism: 2 delay: 10s failure_action: rollback restart_policy: condition: on-failure
- Resource Distribution
services: app: deploy: placement: constraints: - node.role == manager resources: limits: cpus: '0.50' memory: 512M
- Load Balancing
services: app: deploy: endpoint_mode: dnsrr update_config: order: start-first
Common Optimization Patterns
1. Resource Allocation
services:
app:
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M
restart_policy:
condition: on-failure
max_attempts: 3
2. Volume Optimization
services:
app:
volumes:
- app_data:/data:delegated
- ./src:/app:cached
- node_modules:/app/node_modules:delegated
tmpfs:
- /tmp:rw,noexec,nosuid,size=100m
3. Network Optimization
services:
app:
networks:
- app_network
dns:
- 8.8.8.8
- 8.8.4.4
dns_search:
- example.com
extra_hosts:
- "hostname:172.17.0.1"
networks:
app_network:
driver: bridge
driver_opts:
com.docker.network.bridge.enable_icc: "true"
com.docker.network.bridge.enable_ip_masquerade: "true"
Best Practices
- Resource Management
- Set appropriate limits
- Monitor resource usage
- Implement auto-scaling
- Use resource reservations
- Implement health checks
- Storage Optimization
- Use appropriate volume drivers
- Implement caching
- Optimize I/O operations
- Use tmpfs for temporary data
- Implement volume delegation
- Network Performance
- Use appropriate network drivers
- Optimize DNS resolution
- Implement connection pooling
- Use network aliases
- Optimize network settings
- Container Optimization
- Use appropriate base images
- Implement multi-stage builds
- Optimize layer caching
- Use .dockerignore
- Implement container health checks
Performance Monitoring
1. Resource Usage
# Monitor container resources
docker stats
# Check resource limits
docker compose config
# Monitor system resources
docker system df -v
2. Performance Metrics
# Check container performance
docker compose top
# Monitor container processes
docker compose exec app top
# Check container stats
docker compose ps --format json
3. Network Performance
# Test network connectivity
docker compose exec app ping -c 4 google.com
# Check network latency
docker compose exec app curl -o /dev/null -s -w '%{time_total}\n' http://example.com
# Monitor network traffic
docker compose exec app netstat -tulpn
Advanced Optimization Features
1. Container Tuning
services:
app:
ulimits:
nofile:
soft: 65535
hard: 65535
nproc: 65535
tmpfs:
- /tmp:rw,noexec,nosuid,size=100m
shm_size: 64m
cap_add:
- NET_ADMIN
2. Build Optimization
services:
app:
build:
context: .
cache_from:
- myapp:latest
- myapp:${BUILD_CACHE_TAG:-latest}
args:
- NODE_ENV=production
- BUILD_OPTIMIZE=true
target: production
3. Service Scaling
services:
app:
deploy:
replicas: ${REPLICAS:-3}
update_config:
parallelism: 2
delay: 10s
failure_action: rollback
restart_policy:
condition: on-failure
max_attempts: 3
Troubleshooting
- Performance Issues
# Check container stats docker stats # Monitor system resources docker system df -v # Check container logs docker compose logs --tail=100
- Resource Bottlenecks
# Monitor system resources docker system df # Check container processes docker compose top # Analyze resource usage docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
- Network Problems
# Test network performance docker compose exec app curl -o /dev/null -s -w '%{time_total}\n' http://example.com # Check network connectivity docker compose exec app ping -c 4 google.com # Network diagnostics docker compose exec app netstat -tulpn
Integration with Monitoring Tools
- Resource Monitoring
- Prometheus
- Grafana
- cAdvisor
- Node Exporter
- Blackbox Exporter
- Application Performance
- New Relic
- Datadog
- AppDynamics
- Dynatrace
- Elastic APM
- Log Analysis
- ELK Stack
- Fluentd
- Graylog
- Loki
- Promtail
- Container Orchestration
- Docker Swarm
- Kubernetes
- Nomad
- Mesos
- OpenShift
Remember to regularly monitor and adjust your performance configurations based on actual usage patterns and requirements. Implement a comprehensive monitoring strategy and continuously optimize your Docker Compose setup for better performance.