Zero-downtime deployment
The need: when a new version goes to prod, no user should see an error. And if that version is broken, prod must stay on the old one, without human intervention.
Related rules: The pipeline · Dev and prod environments · If the site goes down
1. What the stack file says
infra/docker-compose.prod.yml, the web service:
web:
image: ${CI_REGISTRY_IMAGE}/web:${CI_COMMIT_SHORT_SHA}
environment:
API_URL: https://api.trinity.jostud.com
networks:
- trinity
deploy:
replicas: 2
update_config:
order: start-first
failure_action: rollback| Setting | Effect |
|---|---|
replicas: 2 | Two tasks run at all times. Traefik sends requests to trinity_web, and Swarm spreads them between the two: this is the load balancer |
order: start-first | Swarm starts the new task before stopping the old one. By default, it is the reverse (stop-first), which causes an outage |
failure_action: rollback | If the new task fails, Swarm reverts to the old configuration instead of stopping halfway |
The api service has the same settings. The db service does not: see the pitfalls.
2. What decides a task is ready
Swarm does not only check that the container started: it waits for the image's health check to be green. For the web, it is in web/Dockerfile.prod:
# Docker marque le conteneur "healthy" seulement si /api/health répond.
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
CMD wget -qO- http://127.0.0.1:3000/api/health || exit 1And for the API, in api/Dockerfile.prod:
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1As long as the health check has not passed, the old task keeps answering.
Why is the health check in the image, not in the stack file?
It travels with the image: the same check applies wherever the image runs, and the team writing the application chooses how to prove it is alive.
3. How Traefik follows the change
Traefik sends requests to http://trinity_web:3000. That name is the Swarm service's, not a container's: Docker resolves it to a virtual address that spreads traffic over the service's healthy tasks. When the old task stops, traffic moves to the new one without Traefik needing to be reconfigured.
4. What the job checks
.gitlab-ci.yml, the deploy-prod script:
script:
- docker stack deploy --detach=false --with-registry-auth -c infra/docker-compose.prod.yml trinity
- test "$(docker service ls --format '{{.Image}}' | grep -c "$CI_COMMIT_SHORT_SHA")" = 3
- curl -fsS https://api.trinity.jostud.com/health
- curl -fsS https://trinity.jostud.com/api/health
- curl -fsS -o /dev/null https://docs.trinity.jostud.com/--detach=false: the command waits for Swarm to finish, success or rollback. Without this option, it would return immediately and the job would be green before the end.--with-registry-auth: passes the registry login to Swarm, so it can pull the private images.- The
test … = 3counts the services whose image contains the commit SHA. After a rollback,docker stack deployends without error, but the service runs the old image: the count is below 3, and the job fails. The pipeline turns red, the team is warned. - The
curls check end to end, through DNS, Traefik and HTTPS.
5. This was not the case at first
The task split planned a blue/green deployment: two copies of the application behind Traefik, and a route switch once the new copy is healthy. That required scripting the switch, tracking which colour is active and cleaning up the old one.
The commit feat(infra): deploy with Docker Swarm in prod and Compose in dev replaced that plan with Swarm: start-first + health check + rollback give the same result without a home-made script. See Technical choices.
Pitfalls
The database is not updated with zero downtime
The db service uses Swarm's default behaviour. Changing its image (moving to PostgreSQL 18, for instance) stops it before restarting it. And two PostgreSQL instances cannot use the same volume at once: start-first would be dangerous here.
A rollback does not undo migrations
The new API container runs migrate before its health check. If the new version then fails, Swarm goes back to the old image, but the database keeps the new structure. See Database.
Two replicas, one machine
api and web run as two instances, updated one at a time. During the update, there are briefly three. If one task crashes, the other keeps answering. But if the machine itself goes down, nothing is left: see Limits.
What to remember
start-first: the new version answers before the old one stops.- The image's health check decides when the new version is ready.
rollback+ the SHA check: a broken version does not stay in prod, and the pipeline reports it.