Skip to content

Series

Zero-Downtime Deployments for Node Apps With Docker Compose

Build a small pipeline that brings every push online by itself: GitHub or GitLab tests and builds, your server fetches the new version and swaps it in while visitors keep clicking, with no gap in between.

Beginner11 parts · about 4.5 h

Eleven parts, from a small guestbook on your computer to a server that updates itself. You teach the app to say whether it is healthy and to finish its work before it leaves, pack it into an image, and let GitHub or GitLab test and build it on every push; wherever the two differ, you pick yours once and see only that. Then you set up a server of your own, put Caddy in front for HTTPS, and write the short script at the heart of the series: it starts the new version beside the old one, waits until it is healthy, moves the visitors over and only then lets the old one go. A timer makes the server look for new versions by itself, a broken version never reaches your visitors, one click brings an older version back, the database changes without an outage, and a chat message tells you how each deploy went. All of it with Docker Compose and about eighty lines of shell, and nothing in it is magic.

Start with part 1

Quest line

  1. A Guestbook Worth DeployingSee how the whole pipeline will work, then build the small app it deploys: a guestbook on Node.js with a PostgreSQL database, a test, and a first commit on GitHub or GitLab.25 min
  2. Health Checks and Graceful ShutdownTeach the guestbook the two things every deploy will ask of it: to say honestly whether it is ready for visitors, and to finish its open requests before it leaves.20 min
  3. The App in a ContainerPack the guestbook into a Docker image that checks its own health, run it beside its database with Docker Compose, and watch it shut down gracefully when the container is stopped.20 min
  4. A Pipeline That Tests and BuildsLet GitHub or GitLab do the work after every push: run the tests, build the image and put it on a shelf where the server can find it, under two names.25 min
  5. A Server of Your OwnRent a small server and make it ready: a user of its own, a locked door, Docker without root, a domain that points at it and a key that can read your images and nothing else.30 min
  6. Caddy and the First DeployPut the guestbook online: behind Caddy, which fetches the HTTPS certificate by itself, with its secrets kept on the server. Then update it the usual way and count the visitors who get an error.25 min
  7. Swapping Containers Without a GapWrite the deploy script that closes the gap: it starts the new container beside the old one, waits until it is healthy, moves the visitors over and only then lets the old one go.30 min
  8. The Server Fetches New Versions ItselfLet the server look for a new image once a minute and run the deploy script by itself, with a systemd timer. From now on a push is all it takes, and nothing outside can reach into your server.20 min
  9. When a Deploy Goes WrongWatch a broken version fail without a single visitor noticing, teach the script to remember an image that failed, and build a way back: one click names an older image production again.25 min
  10. Changing the Database Without DowntimeDuring every swap two versions of the app share one database. Learn the rule that keeps both working, and rename a column in three small deploys instead of one risky one.25 min
  11. A Message After Every DeployThe server deploys by itself, so nobody is watching. Let the deploy script post a line to your chat when a version goes live and when one fails, and look back at the whole pipeline.15 min