How to Set Up Blue-Green Deployments
Blue-green deployment is a release strategy that reduces downtime and risk by running two identical production environments. At any time, one environment (blue) serves live traffic while the other (green) is idle or receiving the new release. When ready, you switch traffic from blue to green instantly. This technique works exceptionally well on Breeze instances.
How Blue-Green Deployment Works
- Blue is the current live environment serving all traffic
- Deploy the new version to the Green environment
- Run smoke tests and validation against Green
- Switch the load balancer or reverse proxy to point at Green
- Green is now live; Blue becomes the standby for rollback
Setting Up with Docker Compose
Create two compose files or use profiles for blue and green environments:
# docker-compose.blue.yml
services:
app-blue:
image: myapp:current
ports:
- "8081:8080"
environment:
- APP_ENV=production
- APP_COLOR=blue
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 10s
retries: 3
# docker-compose.green.yml
services:
app-green:
image: myapp:next
ports:
- "8082:8080"
environment:
- APP_ENV=production
- APP_COLOR=green
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 10s
retries: 3
Nginx Configuration for Traffic Switching
Use an Nginx upstream block to control which environment receives traffic:
upstream app_backend {
server 127.0.0.1:8081; # Blue (active)
# server 127.0.0.1:8082; # Green (standby)
}
server {
listen 80;
server_name app.yourdomain.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Deployment Script
Automate the blue-green switch with a bash script:
#!/bin/bash
CURRENT=$(grep -c "8081" /etc/nginx/conf.d/app.conf | grep -v "#")
if [ "$CURRENT" -gt 0 ]; then
TARGET="green"
NEW_PORT=8082
OLD_PORT=8081
else
TARGET="blue"
NEW_PORT=8081
OLD_PORT=8082
fi
echo "Deploying to $TARGET environment..."
# Start the new environment
docker compose -f docker-compose.${TARGET}.yml up -d
# Wait for health check
echo "Waiting for health check..."
for i in $(seq 1 30); do
if curl -sf http://localhost:${NEW_PORT}/health > /dev/null; then
echo "Health check passed!"
break
fi
sleep 2
done
# Switch Nginx to the new environment
sed -i "s/server 127.0.0.1:${OLD_PORT}/# server 127.0.0.1:${OLD_PORT}/" /etc/nginx/conf.d/app.conf
sed -i "s/# server 127.0.0.1:${NEW_PORT}/server 127.0.0.1:${NEW_PORT}/" /etc/nginx/conf.d/app.conf
nginx -s reload
echo "Traffic switched to $TARGET"
Rollback Procedure
If something goes wrong, rollback is instant because the old environment is still running:
# Simply switch Nginx back to the previous environment
# No redeployment needed — the old containers are still live
Best Practices
- Always run health checks on the new environment before switching
- Keep the old environment running for at least one hour after switch
- Use database migration strategies compatible with both versions
- Automate the switch with a deployment script or CI/CD pipeline
- Monitor error rates closely during and after the transition
Blue-green deployments on your Breeze server provide confidence that every release can be instantly rolled back if issues arise.