The Problem
Running docker compose down && docker compose up causes downtime. For production services, you need a strategy that maintains availability during updates.
Method 1: Docker Compose Rolling Update
# Pull new images
docker compose pull
# Recreate only changed services
docker compose up -dDocker Compose recreates containers one at a time when using up -d after pulling new images.
Method 2: Blue-Green with Compose Profiles
services:
app-blue:
image: myapp:v1
profiles: ["blue"]
ports:
- "3001:3000"
app-green:
image: myapp:v2
profiles: ["green"]
ports:
- "3002:3000"# Start new version
docker compose --profile green up -d
# Test green
curl http://localhost:3002/health
# Switch Nginx upstream to green
# Stop old version
docker compose --profile blue downMethod 3: Docker Swarm Rolling Update
docker service update --image myapp:v2 --update-parallelism 1 --update-delay 10s myappHealth Check Integration
services:
app:
image: myapp:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
deploy:
update_config:
order: start-first # Start new before stopping oldAutomated Update Script
#!/bin/bash
cd /opt/myapp
docker compose pull
docker compose up -d --remove-orphans
docker image prune -f
echo "Updated at $(date)" >> /var/log/docker-updates.log