Zero-downtime deploys with Docker Swarm on one server
Docker Swarm can replace a container without dropping a request, even on one server. The settings that make it work, the healthcheck that gates it, and the proxy detail that still drops requests if you miss it.
Docker Swarm can replace a running container without dropping a request, and it works on a single server: docker swarm init turns one machine into a one-node swarm. Swarm brings rolling updates, healthchecks and automatic rollback, all built into Docker. Out of the box, though, it stops the old container first. This guide covers the settings that change that, what the healthcheck has to prove, and the proxy detail that still drops requests if you miss it.
Swarm's defaults stop first
A Swarm service's update_config defaults to order: stop-first and failure_action: pause. So an update stops the old task before the new one starts, and an update that fails stops halfway, with nothing rolled back. Plain docker compose up -d has the same gap: it recreates the container, so the old one stops before the new one starts.
The settings that make it zero-downtime
The following stack file runs one app behind Traefik with the settings this guide explains. Deploy it with docker stack deploy -c stack.yml app.
services:
web:
image: ghcr.io/owner/app:sha-860c190
healthcheck:
test: ["CMD-SHELL", "wget -qO- http://localhost:3000/up || exit 1"]
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
stop_grace_period: 30s
deploy:
replicas: 1
update_config:
parallelism: 1
order: start-first
failure_action: rollback
monitor: 30s
rollback_config:
parallelism: 1
order: start-first
labels:
- traefik.enable=true
- traefik.swarm.lbswarm=true
- traefik.http.routers.app.rule=Host(`app.example.com`)
- traefik.http.routers.app.entrypoints=websecure
- traefik.http.routers.app.tls.certresolver=le
- traefik.http.services.app.loadbalancer.server.port=3000
networks: [public]
networks:
public:
external: trueorder: start-firststarts the new task beside the old one, and stops the old one only once the new one is running.healthcheckdecides when "running" is. A task with a healthcheck stays in Swarm's starting state until the check passes, so start-first waits for it.failure_action: rollbackputs the previous version back if the new one fails.monitoris how long Swarm keeps watching each new task for failure after it starts.rollback_configneedsstart-firsttoo, or the rollback itself has a gap.stop_grace_periodis how long the old task has betweenSIGTERMand being killed. Docker's default is 10 seconds. Your app should stop accepting new connections onSIGTERMand finish the requests it's serving.- The labels are service labels, under
deploy, which is where Traefik's Swarm provider reads them. The entrypoint and certificate resolver names must match your Traefik configuration.lbswarmis explained below.
What the healthcheck has to prove
The healthcheck is the gate, so it has to fail until the app can really serve. An endpoint that returns 200 as soon as the web server is listening, before the app has connected to its database or warmed its caches, lets traffic through too early.
It runs inside the container, so it can only use tools the image has. curl and wget aren't in every base image. A small script in the app's own language works anywhere.
We watched a start-first update on a one-node swarm with a healthcheck that fails its first probe. The new task stayed in Starting while its health was still starting, the old task kept running, and Swarm stopped the old one within a second of the new one turning healthy:
t=02s new: Starting (health: starting) old: Running (healthy)
t=12s new: Starting (health: starting) old: Running (healthy)
t=13s new: Running (health: healthy) old: Running (healthy)
t=14s new: Running (health: healthy) old: stoppedThe proxy can still drop requests
By default, Traefik's Swarm provider sends traffic to the tasks' own IP addresses, which it learns by polling Docker. The polling interval, refreshSeconds, defaults to 15 seconds. After Swarm stops the old task, Traefik can keep sending requests to its address until the next poll, and those requests fail.
traefik.swarm.lbswarm=true sends traffic to the service's virtual IP instead and leaves the balancing to Swarm, which takes a stopped task out straight away. In our own testing, routing to task IPs failed requests for a moment during a redeploy, and the virtual IP didn't. The trade-off is that Traefik no longer does any load balancing of its own for that service, so features such as sticky sessions don't apply.
When start-first can't work
- A service that owns a volume, such as a database. Two tasks would write to the same data, so use
stop-firstand accept a short gap, or schedule the update. - A service that publishes a port in host mode. Two tasks can't listen on one host port.
- Resource reservations. During the overlap, both tasks need room on the node. If their reservations don't fit, the new task stays pending. Limits don't reserve anything, so they don't block the update.
- Database migrations. For a few seconds, the old and new versions run against the same database. A migration the old version can't work with breaks it during the overlap, so add columns first and remove them in a later release.
Test it
Run the following loop against the app while you deploy a new version from another terminal. It prints a line only when a request doesn't return 200, so a quiet loop means nothing dropped.
while true; do
code=$(curl -s -o /dev/null -w '%{http_code}' https://app.example.com/up)
[ "$code" = 200 ] || echo "$(date +%T) $code"
sleep 0.2
done000 means the connection failed, and 502 or 504 means Traefik couldn't reach a task. Echowa deploys with these settings. Across a redeploy of a test app, this kind of loop counted 1,196 requests with none failed, and 1,561 with none failed on a later run.
With Echowa
Echowa is a self-hosted deploy tool for one server, built on exactly this: Docker Swarm and Traefik. Every app it deploys gets these settings without a stack file:
- Start-first updates and rollbacks, one task at a time, rolled back on failure, with a 30-second monitor window.
- The healthcheck command from the app's form, with a 10-second interval, a 5-second timeout, a 30-second start period and 3 retries.
- The
lbswarmlabel on every app's route. - Stop-first for apps that need it: a database with a volume, or one open on a host port.
Each deploy records the image digest it ran and whether it succeeded, failed or was rolled back. Deploy a Docker image from GHCR to your own server walks through a first app, and the install guide covers the server.
curl -fsSL https://get.echowa.dev/install.sh | sudo bash