optimize-docker-compose-resources
Docker Compose Resource Optimization
Configures resource allocation, constraints, and scaling for PHP application stacks.
Resource Allocation Overview
┌─────────────────────────────────────────────────────────────────┐
│ PHP STACK RESOURCE DISTRIBUTION (4GB Host) │
├─────────────────────────────────────────────────────────────────┤
│ PHP-FPM: ████████████████████░░░░░░░░░░ 1024MB (25%) │
│ MySQL: ████████████████████████░░░░░░ 1536MB (38%) │
│ Redis: ████████░░░░░░░░░░░░░░░░░░░░░░ 512MB (13%) │
│ Nginx: ████░░░░░░░░░░░░░░░░░░░░░░░░░░ 256MB (6%) │
│ RabbitMQ: ████████░░░░░░░░░░░░░░░░░░░░░░ 512MB (13%) │
│ System: ████░░░░░░░░░░░░░░░░░░░░░░░░░░ 256MB (6%) │
└─────────────────────────────────────────────────────────────────┘
Recommended Resource Limits
| Service | Memory Limit | Memory Reservation | CPU Limit | CPU Reservation |
|---|---|---|---|---|
| PHP-FPM | 512MB-1GB | 256MB | 1.0 | 0.25 |
| Nginx | 128MB-256MB | 64MB | 0.5 | 0.1 |
| MySQL | 1GB-2GB | 512MB | 1.5 | 0.5 |
| PostgreSQL | 1GB-2GB | 512MB | 1.5 | 0.5 |
| Redis | 256MB-512MB | 128MB | 0.5 | 0.1 |
| RabbitMQ | 512MB-1GB | 256MB | 1.0 | 0.25 |
Service Configurations
PHP-FPM
php-fpm:
deploy:
resources:
limits: { cpus: "1.0", memory: 1024M }
reservations: { cpus: "0.25", memory: 256M }
replicas: 2
tmpfs: ["/tmp:size=64M"]
healthcheck:
test: ["CMD", "php-fpm-healthcheck"]
interval: 30s
timeout: 5s
start_period: 10s
MySQL
mysql:
deploy:
resources:
limits: { cpus: "1.5", memory: 2048M }
reservations: { cpus: "0.5", memory: 512M }
shm_size: "256m"
command: >
--innodb-buffer-pool-size=1G --innodb-log-file-size=256M
--max-connections=200 --tmp-table-size=64M
ulimits:
nofile: { soft: 65536, hard: 65536 }
Redis
redis:
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
reservations: { cpus: "0.1", memory: 128M }
command: redis-server --maxmemory 384mb --maxmemory-policy allkeys-lru --save "" --appendonly no
tmpfs: ["/data:size=384M"]
RabbitMQ
rabbitmq:
deploy:
resources:
limits: { cpus: "1.0", memory: 1024M }
reservations: { cpus: "0.25", memory: 256M }
environment:
RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.6
RABBITMQ_DISK_FREE_LIMIT: 128MB
Logging Configuration
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
| Service | max-size | max-file | Total Disk |
|---|---|---|---|
| PHP-FPM | 10m | 3 | 30MB |
| Nginx | 20m | 5 | 100MB |
| MySQL | 10m | 3 | 30MB |
| Redis | 5m | 2 | 10MB |
tmpfs for Ephemeral Data
tmpfs:
- /tmp:size=64M,mode=1777
- /app/var/cache:size=128M
- /app/var/log:size=32M
Benefits: faster I/O, no disk writes, auto-cleaned on restart.
shm_size for Databases
postgres:
shm_size: "256m" # shared_buffers, WAL buffers, lock tables
mysql:
shm_size: "256m" # InnoDB buffer pool, temp tables
ulimits Configuration
| ulimit | Default | Recommended | Purpose |
|---|---|---|---|
| nofile | 1024 | 65536 | Max open files/sockets |
| nproc | 1024 | 4096 | Max processes |
| memlock | 64KB | unlimited | Memory locking (Redis) |
Scaling with Replicas
php-fpm:
deploy:
replicas: 3
endpoint_mode: dnsrr
resources:
limits: { cpus: "0.5", memory: 512M }
# Total: 1.5 CPU, 1536M memory across 3 replicas
# Nginx resolves php-fpm to all replicas via DNS round-robin
Before/After Comparison
| Metric | No Limits | With Limits | Improvement |
|---|---|---|---|
| OOM kills | Frequent | Rare | Predictable |
| Noisy neighbor | Common | Prevented | Isolated |
| Memory waste | 30-50% | < 10% | -80% waste |
| Log disk usage | Unbounded | < 200MB | Controlled |
| Recovery time | Minutes | Seconds | Faster restarts |
Generation Instructions
- Inventory services: List all containers in the stack
- Assess host resources: Total CPU and memory available
- Allocate proportionally: Database > App > Cache > Proxy
- Set limits and reservations: Limits cap usage, reservations guarantee minimum
- Configure logging: Prevent unbounded disk growth
- Add health checks: Enable automatic recovery
- Test under load: Verify no OOM kills or throttling
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12