Zero-Downtime Deployment
This guide covers blue-green deployment for production environments where downtime is not acceptable.Looking for simple deployment? See the main README for standard Docker Compose deployment.
Overview
Blue-green deployment runs two versions of stateless services simultaneously:- Blue and Green environments alternate as active/standby
- New version starts alongside the current one
- Traffic switches only after health checks pass
- Old version is drained and removed
Requirements
- Docker and Docker Compose
- At least 12-16 GB RAM (runs 2x services during deployment)
- Services must respond to health checks within 5 minutes
Quick Start
Commands
deploy.sh
| Command | Description |
|---|---|
deploy <version> | Pull and deploy specified version with zero downtime |
status | Show current deployment status and health |
rollback | Revert to the previous version |
cleanup | Remove inactive containers |
reset | Remove ALL blue-green containers and return to normal mode |
help | Show usage information |
Configuration
Environment variables to customize deployment:| Variable | Default | Description |
|---|---|---|
HEALTH_CHECK_TIMEOUT | 300s | Max time to wait for health checks |
HEALTH_CHECK_INTERVAL | 3s | Interval between health checks |
DRAIN_TIMEOUT | 30s | Time to drain old containers before removal |
How It Works
Architecture
Deployment Flow
- Pull images - Download specified version from GHCR
- Detect current state - Determine which color (blue/green) is currently active
- Start new version - Run the new containers alongside existing ones
- Health checks - Wait for all new services to report healthy
- Traffic switch - Caddy automatically routes to healthy backends
- Drain old version - Wait for in-flight requests to complete
- Cleanup - Remove old containers
Files
| File | Purpose |
|---|---|
compose.yml | Base configuration for all services |
compose.blue.yml | Blue environment overlay (container names, network aliases) |
compose.green.yml | Green environment overlay |
scripts/deploy.sh | Deployment automation script |
.deployment-color | Tracks current active deployment (auto-generated) |
Database Migrations
Database changes require special handling since the database is shared between blue and green environments. Both old and new versions of the application must work with the database during the transition period.Guidelines
- Backward-compatible migrations only - New code must work with old schema, old code must work with new schema
- Expand-contract pattern - Split breaking changes into multiple deployments:
- Deploy 1 (Expand): Add new columns/tables, keep old ones
- Deploy 2 (Migrate): Application uses new structure
- Deploy 3 (Contract): Remove old columns/tables after confirming success
Examples
| Change | Safe Approach |
|---|---|
| Add column | Add as nullable or with default value |
| Remove column | First deploy code that stops using it, then remove |
| Rename column | Add new column → migrate data → update code → remove old |
| Change type | Add new column with new type → migrate → update code → remove old |
Migration Workflow
Non-Backward-Compatible Changes
If a migration cannot be made backward-compatible:- Schedule a maintenance window
- Use standard deployment with downtime
- Or coordinate a multi-phase rollout over several deployments
Rollback
If a deployment fails or you need to revert:Troubleshooting
Health checks timing out
Deployment failed
Check service health
Memory issues during deployment
Blue-green deployment temporarily runs 2x services. If you run out of memory:- Increase available RAM to at least 12-16 GB
- Or use standard deployment instead (with brief downtime)
When to Use
| Scenario | Recommendation |
|---|---|
| Development / Testing | Standard deployment |
| Low traffic, scheduled maintenance OK | Standard deployment |
| Production, zero-downtime required | Blue-green deployment |
| High availability requirements | Blue-green deployment |
Limited server resources (<12 GB RAM) | Standard deployment |