From Portainer git stacks to docker rollout over SSH
In February I wrote about deploying Portainer git stacks through Portainer's API(/notes/portainer-api-deploy). Since October none of my stacks deploy that wa...
In February I wrote about deploying Portainer git stacks through Portainer's API. Since October none of my stacks deploy that way. Each stack is plain Docker Compose in a folder on the server, deployed over SSH:
- A push to main makes CI build an image tagged with the commit and check that it boots.
- The deploy script copies the compose file to the server, sets the new tag in the
.envthere (keeping the previous one), and runsdocker rollouton the web service. docker rolloutstarts a new container beside the old one. Traefik routes to it once its health check passes, and the old one is drained and removed.- The script checks the version on the health endpoint and runs smoke tests. A failure rolls the previous image back out.
I left Portainer for the rollout. Its redeploy recreates the containers, so every deploy had a gap. A rollout has none, and a build that never becomes healthy leaves the old container serving.
Draining needed more care than I expected. I measured hung requests during rolls on two sites: with no drain, 3 of 311 requests hung in a roll; with a fixed 8-second wait, 2 of 289, because Traefik sometimes kept routing to the old container for longer than that. What worked was a drain hook that makes the health endpoint return 503 and then waits until no request has arrived for 3 seconds: 0 of 1,946 across eight rolls.
Databases, and services that publish host ports, still run as a single container and restart with docker compose up -d.