🐳 Docker Advanced

How do you implement zero-downtime deployments with Docker?

Answer

Zero-downtime deployments update running containers without dropping requests. Strategies: (1) Docker Swarm rolling updates: docker service update --image myapp:2.0 --update-parallelism 1 --update-delay 30s --update-failure-action rollback myapp — updates one replica at a time, waits 30s between replicas, automatically rolls back if new containers fail health checks; (2) Blue-Green deployment: run two identical environments (blue=current, green=new). Deploy new version to green; run tests; switch the load balancer/reverse proxy to point to green; if issues, switch back to blue instantly. Containers: have two sets of containers with different labels/names; switch Nginx/HAProxy upstream or DNS; (3) Canary deployment: route a percentage of traffic to the new version (10%), monitor metrics, gradually increase percentage; (4) Reverse proxy integration: Nginx upstream reloads (nginx -s reload) are zero-downtime — Nginx finishes in-flight requests before removing old workers. In Docker Compose, update the service definition and run docker compose up -d — Compose recreates containers one at a time for services with multiple replicas; (5) Health check gates: use healthchecks to ensure new containers are ready before the old ones are removed. Application requirements: containers must handle SIGTERM gracefully (finish in-flight requests), implement readiness endpoints, and be stateless.