Zero-Downtime Deploys on a Single VPS: Blue-Green Explained

Every deploy has a gap. You stop the old version, start the new one, and for a few seconds the site is down or half-updated. On a single VPS that gap feels unavoidable, because there is only one machine. Blue-green deploys remove it with a simple idea: run two copies of the app, send traffic to one, update the other, then flip the traffic. This guide covers zero-downtime deploys single vps setups can run with Docker and a reverse proxy, no cluster needed.

Blue is the live copy. Green is the idle copy. A deploy builds the new version into green, waits for its health check to pass, then tells the proxy to send traffic to green. Blue keeps running until the flip succeeds, so a bad build means the flip never happens and users never see it. Rollback is the same move in reverse: point the proxy back at blue.

Zero-downtime deploys single VPS: how blue-green works

The setup needs three pieces. A blue-green deployment single server setup needs two app containers, one for blue and one for green, each on its own port. A reverse proxy, usually Nginx, that forwards traffic to the live port. And a deploy script that builds the new image, starts it on the idle port, checks its health, and rewrites the proxy config to point at it. The proxy reload takes milliseconds and drops no connections, because Nginx reloads its config without stopping the worker processes that serve current requests.

The data layer needs care. Both copies must talk to the same database, and the database migration must work with both the old code and the new code at once. The safe pattern is expand then contract: first deploy a migration that adds the new column without removing the old one, then deploy the code that uses it, then remove the old column in a later deploy. A migration that renames a column in one step will break the copy that still expects the old name.

Set up the two containers and the proxy

Run the app twice with different names and ports. The Compose file below defines both services from the same image, with only the port and the container name differing:

services:
  blue:
    image: my-app:${TAG:-latest}
    ports:
      - "3001:3000"
  green:
    image: my-app:${TAG:-latest}
    ports:
      - "3002:3000"

Nginx sits in front and proxies to the live port. A minimal server block points at blue at first:

upstream app {
  server 127.0.0.1:3001;
}
server {
  listen 80;
  server_name example.com;
  location / {
    proxy_pass http://app;
    proxy_set_header Host $host;
  }
}

An nginx blue green deploy flips traffic by rewriting one upstream line, and the upstream block is the only line the deploy script changes. When the flip happens, it rewrites 3001 to 3002 and runs nginx -t && nginx -s reload. The reload is graceful, and in-flight requests finish on the old workers. Our guide to zero-downtime deploys with an Nginx reverse proxy and Cloudflare covers the same reload pattern with a CDN in front, and the guide to Nginx server blocks for multiple websites on one VPS shows how the proxy layout fits a host with several sites.

Write the deploy script

The script is short, and each step has a check before the next one runs. Build the image, start it on the idle port, wait for the health endpoint, flip the proxy, then stop the old copy:

#!/bin/bash
set -e
LIVE=$(grep -o '300[12]' /etc/nginx/conf.d/app.conf | head -1)
IDLE=3002; [ "$LIVE" = "3002" ] && IDLE=3001
docker pull my-app:latest
docker run -d --name app-$IDLE -p $IDLE:3000 my-app:latest
for i in $(seq 1 30); do
  curl -fs http://localhost:$IDLE/health && break
  sleep 2
done
sed -i "s/300[12]/$IDLE/" /etc/nginx/conf.d/app.conf
nginx -t && nginx -s reload
docker stop app-$LIVE || true
docker rm app-$LIVE || true

Keep the old container running for a few minutes after the flip instead of stopping it at once, so a problem found in the first minutes can be fixed with a single proxy change. Log the live port to a file so the script and your runbook agree on which copy is live. If the health check fails, the script exits before the flip, and the old copy keeps serving traffic while you read the logs.

When blue-green is not the right answer

Blue-green doubles the memory and disk the app needs, because two copies run side by side during a deploy. On a small VPS with a heavy app, that can be too much, and a rolling restart or a canary deploy fits better. A canary sends a small share of traffic to the new version first, which catches errors with fewer users affected, and our guide to blue-green vs canary deployment strategy compares the two in detail. If you move between hosts instead of deploying on one, read our guide to migrating between VPS providers with zero downtime, which uses the same flip-the-traffic idea at the DNS level.

Stateful apps need extra thought too. An app that writes files to its own disk will lose them on the flip unless the files live in a shared volume, and a WebSocket app needs sticky sessions or a shared store so a reconnect lands on the right copy. This zero downtime docker deploy pattern works for any stateless app in a container. Fix those two before your first blue-green deploy, or the first flip will teach you about them in production. Once the pieces are in place, deploys stop being events. You push, the script flips, and the only sign of a deploy is a new version number in the footer.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.