How to update running containers with zero downtime?
· Category: Docker
Short answer
Achieve zero downtime by using a reverse proxy or load balancer to route traffic to new containers while old ones finish processing. Blue-green and rolling deployments are the most common patterns.
Steps
- Start new containers alongside old ones.
- Health check the new containers.
- Update the load balancer to point to the new containers.
- Gracefully drain and stop the old containers.
Example
Using Docker Compose with a blue-green approach:
docker compose up -d --scale web=2 --no-recreate
# Update proxy to new container, then remove old
With Swarm rolling updates:
docker service update --image myapp:v2 --update-parallelism 1 --update-delay 10s web
Tips
- Use health checks before switching traffic.
- Ensure sessions are stored externally if using sticky sessions.
- Set appropriate grace periods for long-running requests.
Common issues
- Sticky sessions may drop if not handled during the switch.
- Database schema changes complicate rolling updates.
- Insufficient health check intervals can route traffic to unhealthy containers.