Day 6 - Docker Optimization & Multi-Environment Setup
🎯 Objectives Achieved
- Analyzed existing Docker setup with VM experience perspective
- Deep comparison VM vs Docker documented
- Optimized Docker Compose with production best practices
- Created multi-environment configurations (dev/prod)
- Tested and validated all configurations
- Comprehensive documentation
📊 Performance Metrics
Deployment Time Comparison
| Environment | Time | Notes |
|---|---|---|
| VM (Week 1) | 1 min 37 sec | Automated script |
| Docker Base | 7.64 sec | Initial measurement |
| Docker + Health Checks | 13-16 sec | With depends_on |
Improvement: Docker 6-12x faster than VM ✅
Resource Usage
Development Mode:
App: 9.19 MB / 1 GB (0.90%)
Prometheus: 20 MB / 1 GB (1.95%)
Total: ~29 MB / 2 GB
Production Mode:
App: 7.88 MB / 512 MB (1.54%)
Prometheus: 19.02 MB / 512 MB (3.71%)
Total: ~27 MB / 1 GB
VM Baseline (Week 1):
OS: 206 MB (just Ubuntu)
App: 1.4 MB (process)
Total: ~207 MB
Insight: Docker containers use ~27MB vs VM ~207MB baseline - 87% less overhead! ✅
🔧 Optimizations Applied
Docker Compose Enhancements
1. Resource Limits
deploy:
resources:
limits:
cpus: '0.5' # Dev: 2.0
memory: 512M # Dev: 1GB
reservations:
cpus: '0.25'
memory: 256M
Why: Prevent resource monopolization, predictable performance
2. Log Rotation
logging:
driver: "json-file"
options:
max-size: "10m" # Dev: 100m
max-file: "3" # Dev: 5
Why: Disk space management, log file control
3. Network Isolation
networks:
app-network: # Application traffic
monitoring-network: # Metrics traffic
Why: Security, traffic separation, clarity
4. Health Checks
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
Why: Automated health monitoring, restart on failure
5. Dependency Management
depends_on:
app:
condition: service_healthy
Why: Ordered startup, Prometheus waits for app to be ready
6. Security Hardening (Production)
security_opt:
- no-new-privileges:true
read_only: true
tmpfs:
- /tmp
Why: Attack surface reduction, privilege escalation prevention
🔄 Multi-Environment Setup
Configuration Strategy
Base (docker-compose.yml):
- Common configuration
- Service definitions
- Networks and volumes
- Environment variables with defaults
Development (docker-compose.dev.yml):
- Debug enabled
- Verbose logging (debug level)
- Generous limits (1GB RAM, 2 CPU)
- Large log files (100MB)
- Hot-reload capable
Production (docker-compose.prod.yml):
- Security hardened
- Minimal logging (error level)
- Strict limits (512MB RAM, 0.5 CPU)
- Read-only filesystem
- Performance optimized
Usage Comparison
Development:
docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d
Production:
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
Result: Same codebase, different runtime configurations ✅
💡 Key Learnings
What Docker Automates (That VM Required Manual Setup)
- Process Isolation
- VM: systemd services, user management
- Docker: Container runtime handles automatically
- Dependency Management
- VM: Manual Go installation, version conflicts possible
- Docker: Exact versions in Dockerfile, reproducible
- Network Isolation
- VM: iptables, firewall rules, manual configuration
- Docker: Network drivers, automatic DNS resolution
- Resource Limits
- VM: cgroups manual configuration
- Docker: Declarative
deploy.resources
- Logging
- VM: rsyslog, manual rotation setup
- Docker: Built-in drivers with rotation
- Health Monitoring
- VM: Custom scripts, systemd restart policies
- Docker: Native healthcheck + restart policies
- Rollback
- VM: Manual process, prone to errors
- Docker: Tag-based, instant (
docker compose down/up)
Deep Understanding Through VM Experience
Before VM Week: “Docker is magic, I don’t understand why”
After VM Week: “Docker automates all these painful manual steps!”
Specific Revelations:
- Creating
appuser→ Docker user namespace - Port management → Container networking
- systemd complexity → Restart policies
- File permissions → Volume mounts
- Deployment complexity → Image layers
Result: Deep appreciation for containerization benefits, not just surface-level usage ✅
📈 VM vs Docker Final Comparison
| Aspect | VM (Manual/Script) | Docker (Optimized) | Winner |
|---|---|---|---|
| Deployment Time | 1min 37sec | 13-16sec | 🐳 Docker (6x) |
| Configuration | 80 lines bash | 45 lines YAML | 🐳 Docker (44% less) |
| Reproducibility | ~80% | 100% | 🐳 Docker |
| Learning Curve | Medium | Low (once understood) | 🐳 Docker |
| Debugging | Complex (systemd logs) | Simpler (docker logs) | 🐳 Docker |
| Rollback | Manual, risky | Instant, safe | 🐳 Docker |
| Multi-env | Multiple scripts | Compose overrides | 🐳 Docker |
| Security | Manual hardening | Declarative options | 🐳 Docker |
| Deep Understanding | High | Medium | 🖥️ VM |
| Production Value | Complex | Simple | 🐳 Docker |
Overall Verdict: Docker wins on every practical metric. VM experience invaluable for understanding why.
🎓 Skills Demonstrated
Technical Skills
Docker & Containers:
- Docker Compose multi-file configurations
- Resource limits and constraints
- Network isolation strategies
- Security hardening (read-only, tmpfs, no-new-privileges)
- Health check implementation
- Log management and rotation
- Multi-environment orchestration
System Administration:
- Resource planning (CPU/RAM allocation)
- Security best practices
- Performance optimization
- Operational procedures
DevOps Practices:
- Infrastructure as Code
- Configuration management
- Environment separation (dev/prod)
- Documentation standards
🔄 Before/After Comparison
Before Optimizations
services:
app:
ports:
- "8000:8000"
environment:
- PORT=8000
prometheus:
ports:
- "9090:9090"
Issues:
- No resource limits
- No log rotation (disk filling risk)
- Single network (no isolation)
- No health monitoring
- No multi-env support
After Optimizations
# Base + overrides system
# Resource limits: CPU/RAM
# Log rotation: 10MB, 3 files
# Networks: app + monitoring isolated
# Health checks: automated monitoring
# Security: hardened in production
# Multi-env: dev/prod configurations
Improvements:
- ✅ Predictable resource usage
- ✅ Disk space protected
- ✅ Network isolation
- ✅ Automated health monitoring
- ✅ Environment-specific configs
- ✅ Production-ready security
📋 Deliverables
Documentation Created
- ✅
docs/phase1/week2/Day6_Docker_VM_Deep_Comparison.md - ✅
docs/phase1/week2/Day6_Docker_Optimization.md(this file) - ✅
deployments/README.md(usage guide)
Configurations Created
- ✅
docker-compose.yml(optimized base) - ✅
docker-compose.dev.yml(development overrides) - ✅
docker-compose.prod.yml(production overrides) - ✅
.env.example(environment template)
Tests Performed
- ✅ Base configuration validation
- ✅ Development mode tested
- ✅ Production mode tested
- ✅ Resource limits verified
- ✅ Security options verified
- ✅ Health checks validated
🚀 Next Steps
Tomorrow (Day 7):
- Add PostgreSQL container
- Implement data persistence (volumes)
- Database migrations
- Backup strategies
This Week:
- Deploy Docker to AWS EC2
- Production monitoring setup
- Performance testing
- Week 2 retrospective
💭 Personal Reflections
What Went Well
- Multi-env setup cleaner than expected
- Security hardening straightforward
- Documentation comprehensive
Challenges Overcome
- YAML indentation errors (learning experience)
- Volume mount paths (Prometheus)
- Understanding security options