16 min read
Zero-downtime deployment with Docker and nginx
In one sentence: Zero-downtime deployment means changing your API’s version without a single visitor getting an error. In the shop, two copies take turns behind nginx: the deployment changes one while the other answers, and the one that leaves shuts down without cutting anyone off. On our practice server, from about 12 seconds of 502 per deployment to almost none.
What you will learn
- Explain a rolling deployment with two copies, and why each new copy has to be healthy, plus 10 seconds more, before the other is touched.
- Make the API shut down without cutting anyone off, and know which requests nginx repeats on the other copy and which it doesn’t.
- Turn off the Docker proxy that spoils the handover, and measure a deployment with and without downtime on your server.
- Tell a rolling deployment from blue-green and canary, and write migrations that serve two versions at once.
Before you start, read: 03-scaling.md phase-8/06-ci-cd.md
The problem
Section titled “The problem”Ana pushes a small change, the shop’s title, and Phase 8’s timer deploys it on its own. While the API changes, everyone who visits gets a 502.
We measured it again, with Phase 8’s script, while a loop kept asking for products and logging in without pause: about 12 seconds of 502, ten of them without a single good answer. Imagine Marta was paying right then: with a 502, she doesn’t know whether she was charged.
In Phase 8 we promised to deploy without downtime, with two copies that take turns. This is that lesson.
The analogy
Section titled “The analogy”The analogy
Think of a supermarket with two checkouts. The cashier at checkout 2 is due to be relieved: she puts up the “closed” sign, new customers go to checkout 1, and she serves everyone left in her line. Only then does she hand the seat to her colleague. Then checkout 1 does the same. The supermarket never runs out of open checkouts.
Where the analogy breaks down
- A customer can arrive just as the sign goes up, and on a server that connection gets cut: it’s the rare race at the end.
- nginx doesn’t look at any sign: it finds out when a copy refuses its connection.
- During the handover, the old version and the new one both answer, with a single database.
How it really works
Section titled “How it really works”Twelve seconds of 502, measured again
Section titled “Twelve seconds of 502, measured again”A piece of that measurement, with step 39: how many requests came out every second with each code (trimmed, …).
13:24:31: GET_200x5 POST_200x3313:24:32: GET_200x1 GET_502x4 POST_200x5 POST_502x3813:24:33: GET_502x5 POST_502x88…13:24:43: GET_502x7 POST_200x1 POST_502x17213:24:44: GET_200x6 GET_502x1 POST_200x49 POST_502x3Those are the two windows from Phase 8: the container being replaced, and the 10 seconds in which nginx sets aside a copy that failed. With continuous traffic, some request always stumbles on the new copy before Node is listening.
With one copy, nginx has nobody to hand anything to. For a Zero-downtime deploymentReleasing a new version without users noticing any interruption, by updating one copy at a time while the others keep serving. Requests already in progress finish before anything is switched off.Go to definition, there always has to be one answering.
Two fixed copies: api-a and api-b
Section titled “Two fixed copies: api-a and api-b”Since step 40, production runs two copies of the API:
# The two copies of the API, each on its own port, only on the server itself (127.0.0.1): nginx# talks to them from there (they are the two server lines of its upstream), and the internet# doesn't reach them. A fixed port for each one, and not a range with --scale: this way deploy.sh# knows which copy it is changing.api-a: <<: *api ports: - '127.0.0.1:3000:3000'api-b: <<: *api ports: - '127.0.0.1:3001:3000'<<: *api: copy here what the two share, written once above under the name&api(a YAML anchor).3000and3001: the two ports nginx’supstreamhas had since Phase 8. nginx doesn’t change.
These are two named services, not the --scale from lesson 3: with a fixed port each, the script knows which copy it’s changing.
One copy at a time
Section titled “One copy at a time”deploy.sh does what Phase 8’s did, but changes the copies one by one:
- Backup and migrationsboth old copies answer
then
- api-b, with the new imageapi-a answers alone
healthy
- 10 seconds of marginfor nginx
then
- api-a, with the new imageapi-b answers alone
healthy
- The workerand out go the old images
It’s a rolling deployment:
# First api-b and then api-a (the other way round if the single-copy API is on 3001: old-api, above).# The first deployment of this step arrives at a server with that old API on one of the two ports;# the first copy uses the other one, which is free, so the old one keeps answering until the first# is healthy. Then, the old one leaves its port to the second: it is stopped (SIGTERM, like any# copy, and it finishes what it has half done) and removed. Only that time: afterwards there is no# api service any more.log "$first_copy, with the new image (the other copy keeps answering)"change "$first_copy" "$image" || roll_back "$first_copy"nginx_pauseif [ -n "$old_api" ]; then log "the single-copy API leaves its port to $second_copy" docker stop "$old_api" > /dev/null docker rm "$old_api" > /dev/nullfilog "$second_copy, with the new image ($first_copy already answers with it)"change "$second_copy" "$image" || roll_back "$second_copy" "$first_copy"change:docker compose up -dfor a single service, with the new image. Docker sends the SignalA short notice the operating system delivers to a process, like “interrupt yourself” or “finish”. Each signal is a number with a name, like SIGTERM, and pressing Ctrl+C in the terminal sends one.Go to definitionSIGTERMto the old container (Phase 7), waits for it to exit and starts the new one.|| roll_back: if it doesn’t get healthy, it goes back to the previous image.old_api: the single copy from steps 37 to 39, only the first time.
api-b goes first because of that first time. The single copy was on 3000 or on 3001: Compose gave it the first free port, and on every deployment it jumped from one to the other (we saw it go from 3000 to 3001 and back).
The first new copy takes the free port and, once it’s healthy, the old one leaves its own port to the second copy. If the old one is on 3001, the script starts with api-a.
The worker goes last: jobs that arrive in the meantime wait in the queue. A deployment takes about 40 seconds.
Ten seconds of margin for nginx
Section titled “Ten seconds of margin for nginx”Between one copy and the other, it waits:
# nginx gives a copy one failure for 10 seconds (fail_timeout, nginx/shop.conf) from the last time# it failed. While it was being changed, the new copy failed for nginx; now it is healthy, but that# last failure may be less than 10 seconds old, and nginx wouldn't send it anything yet: if the other# one stopped now, nginx would have nowhere to send the requests (502). So, before touching the# other one, 10 seconds more.nginx_pause() { sleep 10}While api-b was being changed, its connections failed, and nginx set it aside for 10 seconds from the last failure. Docker’s first HealthcheckA command Docker runs every few seconds inside a container to find out whether its program really works, not just whether it’s running. If it ends well, the container is healthy; after several failures in a row, it’s unhealthy. Compose uses it to wait for a service before starting the ones that depend on it.Go to definition comes 10 seconds after starting: api-b can be healthy while nginx is still setting it aside. If api-a stopped then, nginx would have nobody: 502, even for a GET.
In our measurement, from 13:28:02 to 13:28:23: about 10 seconds until healthy and 10 of margin.
Shutting down without cutting anyone off
Section titled “Shutting down without cutting anyone off”The copy that leaves receives SIGTERM. Up to step 39, the shutdown was half done: an open event stream held the copy until it exited after 5 seconds, cutting the stream. Now it does a Graceful shutdownSwitching a program off by letting it finish what it has in hand. It stops accepting new requests, completes the ones in progress and only then closes, instead of cutting them off abruptly.Go to definition:
export async function shutDown( server: Server, resources: Resource[], limitMs: number,): Promise<void> { const until = Date.now() + limitMs; controller.abort();
// 1 and 2. close() stops accepting connections, and calls its function when the last one closed. const closed = new Promise<boolean>((resolve) => server.close(() => resolve(true))); // A keep-alive connection (HTTP/1.1 leaves it open for the next request) doesn't close by // itself. close() closes the ones waiting idle, but not the ones with a request half done: when // it ends, they stay waiting for another. So whatever still arrives through them is answered // with Connection: close (the client doesn't use the connection again), and every 100 ms the ones // left free are closed. prependListener: before the shop, which is the one that writes the // response; afterwards, the headers might already have gone out. server.prependListener('request', (_request, response) => response.setHeader('Connection', 'close'), ); const idle = setInterval(() => server.closeIdleConnections(), 100); const onTime = await Promise.race([closed, sleep(limitMs, false, { ref: false })]); clearInterval(idle); if (!onTime) { // What didn't finish in time is cut: better that than docker stop's SIGKILL. log.warn(`after ${limitMs} ms, requests are still half done: they are cut`); server.closeAllConnections(); }
// 4. One after another, without going over the limit: one that doesn't answer (a hung Redis) // doesn't hold back the others. If one fails, it goes to the log, and the next one follows. for (const resource of resources) { const left = Math.max(until - Date.now(), 0); try { await Promise.race([resource.close(), sleep(left, undefined, { ref: false })]); } catch (error) { log.warn( { reason: (error as Error).message }, 'something did not close properly on shutdown', ); } }}controller.abort()gives notice: the order event streams and the WebSockets, which never end, close right away, and/healthanswers503. Their clients reconnect, and reach the other copy.server.close(…)stops accepting connections: nginx’s next attempt is refused, and nginx hands it to the other copy.- Keep-alive connections, which HTTP/1.1 leaves open for the next request, don’t close by themselves: whatever arrives through them goes out with
Connection: close, and every 100 ms the free ones are closed. - It waits for the half-done requests, 8 seconds at most: Docker waits 10 (
stop_grace_period) before sendingSIGKILL. - At the end, what those requests use, one after another: PostgreSQL, the queue, the order notifications, the cache and the rate limits (
src/server.ts).
- api-a (leaving)stops accepting connections
finishes what it has half done - api-a (leaving) → nginxrefused
- api-b → nginx200
- api-a (leaving)no requests left: closes
PostgreSQL and Redis, and exits
What nginx repeats and what it doesn’t
Section titled “What nginx repeats and what it doesn’t”When a copy fails, nginx tries the other one. Not always:
| What happens to nginx | In its error.log |
Does it repeat it on the other copy? |
|---|---|---|
| The connection is refused | connect() failed (111: Connection refused) |
Yes, a POST too: it never went out |
| It’s accepted and cut after the request is sent | recv() failed (104: Connection reset by peer) or upstream prematurely closed connection |
A GET, yes. A POST, no: 502 |
A POST that already went out may have been carried out, and repeating it could charge twice: it isn’t IdempotentSaid of an operation that leaves things in the same state whether you do it once or ten times. In HTTP, GET, PUT and DELETE are idempotent and POST isn’t, so repeating a POST can create two orders.Go to definition.
A log we added to nginx ($upstream_addr, $upstream_status) shows it. A login while api-a was being changed:
13:25:59.980 POST /v2/session 200 upstream=127.0.0.1:3000, 127.0.0.1:3001 ustatus=502, 200 urt=0.000, 0.087 rt=0.087Two copies tried: 3000 refused the connection (nginx records it as 502) and 3001 answered 200. The time has fractions of a second at the end: they’re milliseconds.
Docker’s proxy accepts and cuts
Section titled “Docker’s proxy accepts and cuts”In step 40’s first measurement, with Docker’s default configuration, the GETs all came out fine, but 5 of 6,679 POSTs got a 502, one or two for each copy changed (our measurement, from an earlier run of the same step). In the error.log, recv() failed (104: Connection reset by peer): the connection was accepted and cut. To publish a port, Docker starts by default a small program, docker-proxy, that listens on 127.0.0.1:3001 and passes every connection on to the container. While the old copy finishes its work, its container is still alive and docker-proxy keeps accepting: it finds nobody listening inside, and cuts.
You turn it off with {"userland-proxy": false} in Docker’s configuration. Then it’s the Linux kernel that carries the connections to the container, and a port with nobody listening refuses them. Without it, 0 of 1,091 GETs and 0 of 6,130 POSTs failed (same measurement), and our English runs of the deployment with the proxy off had zero errors. The API still sees nginx arriving from 172.18.0.1, and the ports on 127.0.0.1 are still closed from outside.
While it’s still on, deploy.sh warns you:
# A warning, stopping nothing. If Docker passes the published ports through docker-proxy (its# "userland proxy", on unless changed), the port of a copy that is shutting down keeps accepting# connections and cuts them: nginx has already sent the request, and it doesn't repeat a POST on the# other copy (502). Without docker-proxy, the connection is refused, and nginx repeats the request# on the other one, even if it's a POST. Docker doesn't say how it is configured without sudo, but# every port that goes through the proxy is a docker-proxy process in plain sight, with its port# (-host-port 3000) on the command line.if pgrep -f 'docker-proxy.*-host-port 300[01] ' > /dev/null 2>&1; then log "warning: the API's ports go through docker-proxy; to deploy without downtime, {\"userland-proxy\": false} in /etc/docker/daemon.json and sudo systemctl restart docker (README)" >&2fiAlmost zero: the race that remains
Section titled “Almost zero: the race that remains”Without docker-proxy, in about 30 copy stops measured, one POST got a 502 once. Most likely: a connection that arrived just as the API stopped listening.
That’s why we say almost zero. Closing that gap takes taking the copy out of the Load balancerWhat spreads requests across several copies of the same program, so that none of them carries everything and, if one goes down, the others keep serving. It can be a program, like nginx with its upstream block, or a service from a cloud provider.Go to definition before stopping it:
- in nginx, mark it
downin theupstreamand reload, withsudoon every deployment; - in NGINX Plus, the paid version, with its API;
- in Kubernetes, a system that spreads containers across servers: it stops sending the copy traffic and waits before stopping it.
The other side is the client: an app that retries a POST with its Idempotency keyA unique value the client sends in the Idempotency-Key header along with an operation, such as creating an order. If it repeats the request with the same key, the server returns the first response instead of doing it again.Go to definition from Phase 4 doesn’t charge twice.
Alive and ready: liveness and readiness
Section titled “Alive and ready: liveness and readiness”/health answers 503 while the copy shuts down, even if the database responds:
// Is the shop fine? The Dockerfile's HEALTHCHECK asks every few seconds. Without the database the// shop can hardly do anything, so it only answers 200 if PostgreSQL replies; if not, 503 Service// Unavailable. And since Phase 9 (lesson 4), 503 also while shutting down (shutdown.ts), even if// the database replies: this copy is leaving. Docker and deploy.sh read it; nginx doesn't ask// /health (it only sees whether the real requests fail).app.get('/health', async (c) => { if (shuttingDown()) return httpProblem(c, 503, 'The shop is shutting down'); try { await pool.query('SELECT 1'); return c.json({ status: 'ok' }); } catch { return httpProblem(c, 503, 'The database is not responding'); }});These are two questions we tend to mix up:
- Is it alive? (liveness): if not, restart it.
- Is it ready? (readiness): if not, don’t send it requests.
A copy that is shutting down is alive, but no longer ready: its 503 is the honest answer for whoever asks. Kubernetes asks the two questions separately, and the shop will too in Phase 10. Docker has a single healthcheck, and a copy that fails it isn’t restarted: it stays unhealthy. Here, Docker and deploy.sh read /health; nginx only looks at the real requests.
If the new copy doesn’t get healthy
Section titled “If the new copy doesn’t get healthy”The script no longer always waits 60 seconds: it gives up as soon as Docker marks the copy unhealthy or the container stops running. If the first new copy fails, only it goes back to the previous image; if the second fails, both go back, one at a time, and if the worker fails, all three. We broke the first copy in two ways, with no error in the loop:
/healthalways503: noticed after 33 seconds, at the third failed healthcheck. Meanwhile it served the other routes fine: nginx doesn’t ask/health.- The API exits on startup: 16 seconds, not at once. Between restarts, the container looks like it’s running most of the time.
At the end, docker image prune deletes the untagged images that no container uses: the new one (main) and :previous stay. Phase 8’s script didn’t do this. Careful: it looks at every image in that server’s Docker, not just the shop’s. If another project runs there, its untagged images that no container uses go too.
Migrations for two versions at once
Section titled “Migrations for two versions at once”MigrationA file with a change to a database’s tables, such as adding a column, that is applied only once and in order, without deleting the data already there. The database itself records in a table which ones have already been applied.Go to definition are applied before touching any copy, and for about 20 seconds an old copy and a new one answer together: two versions of the code with the same schema.
In Phase 8, expand and contract was advice; now it’s mandatory. If you rename name to title in a migration, the old copies fail as soon as it’s applied: at first, both. First expand (add title and write to both columns); in another deployment, contract.
Blue-green and canary
Section titled “Blue-green and canary”Two other ways to deploy without downtime:
- A Blue-green deploymentA way of releasing with two complete environments: blue, with the current version serving users, and green, with the new one. Once green is tested, traffic is switched from one to the other at once. If something goes wrong, you switch back to blue.Go to definition sets up a whole environment with the new version (the green one) next to the one that’s answering (the blue one), tests it and moves all the traffic over at once.
- A canary gives the new version to a part of the visits, say one in twenty, and watches its errors before giving it to everyone.
| Rolling (the shop) | Blue-green | Canary | |
|---|---|---|---|
| How the new version arrives | Copy by copy | All at once | To a part first |
| What extra it needs | A copy that can hold up alone for a while | A whole environment | Splitting by percentage (in nginx, weight) |
| Going back | Another round, about 40 s | Instantly | Remove the new part |
Try it
Section titled “Try it”The goal: measure the 502s of Phase 8’s deployment on your server, switch to two copies and measure again, until you see none.
The outputs are from our course VM (2 CPUs, 2 GiB, Docker 29.9.2, nginx 1.28.3) from October 22, 2026, with a test registry (localhost:5000) instead of ghcr.io. Your times, numbers and IDs will be different. Everything goes on the server, except what says “your computer”; on a VPS, the same.
Prepare the measurement
Section titled “Prepare the measurement”Stop the timer and save your account’s email and password in a file only you can read:
sudo systemctl stop shop-deploy.timercat > ~/.login.json <<'END'{"email":"admin@shop.example","password":"your-account-password"}ENDchmod 600 ~/.login.jsonA correct login is the slowest route (argon2), and the limit only counts failed ones: it can be repeated without stopping. Open nano ~/loop.sh, paste this loop, save (Ctrl + O) and exit (Ctrl + X):
#!/usr/bin/env bash# Measurement loop: for $1 seconds, through nginx (https://shop.example):# 1 loop of GET /v2/products, one every 0.1 s# 4 loops of POST /v2/session (a correct login: argon2), back to back# Each request, one line in $2: time (ms), method, status code, curl total time (s).set -udur="$1"; out="$2"; : > "$out"end=$(( $(date +%s) + dur ))stamp() { local t=$EPOCHREALTIME; printf "%(%T)T.%s" "${t%.*}" "${t#*.}" | cut -c1-12; }get() { while [ "$(date +%s)" -lt "$end" ]; do t="$(stamp)"; r="$(curl -s -o /dev/null -w "%{http_code} %{time_total}" --cacert ~/ca/ca.crt https://shop.example/v2/products)" echo "$t GET $r" >> "$out"; sleep 0.1; done; }post() { while [ "$(date +%s)" -lt "$end" ]; do t="$(stamp)"; r="$(curl -s -o /dev/null -w "%{http_code} %{time_total}" --cacert ~/ca/ca.crt -H "Content-Type: application/json" --data @"$HOME/.login.json" https://shop.example/v2/session)" echo "$t POST $r" >> "$out"; done; }get & for i in 1 2 3 4; do post & done; waitIt’s ours, as is: the get & and post & run at the same time, and every request leaves a line with its time (stamp), its method and its code. Change our VM’s name and certificate to yours from Phase 8, lesson 5, and give it permission. On the free path, with your IP in the sslip.io name:
sed -i 's|shop.example|192-168-5-15.sslip.io|g; s|~/ca/ca.crt|~/authority/ca.crt|g' ~/loop.shchmod +x ~/loop.shWith Let’s Encrypt, your domain and no --cacert: sed -i 's|shop.example|your-domain.com|g; s|--cacert ~/ca/ca.crt ||g' ~/loop.sh, and the same chmod.
Before: Phase 8’s deployment
Section titled “Before: Phase 8’s deployment”On your computer, with your project (~/backend-course/phase-3/shop-api) still at step 39, change the title in src/shop.ts (for example, “Ana and Luis’s shop (version 2)”), push to main and wait for Publish to finish. On the server:
1. A deployment with a single copy
cd /opt/shop(~/loop.sh 60 before.txt & sleep 5; deployment/deploy.sh > before.log 2>&1; wait)awk '{print $2, $3}' before.txt | sort | uniq -cawk '$3 != 200' before.txt | sort | sed -n '1p;$p'What you will see:
332 GET 200 84 GET 502 2538 POST 200 1802 POST 50213:24:32.141 POST 502 0.00706913:24:44.076 POST 502 0.002707The loop runs for 60 seconds and, at 5, the deployment (its output goes to before.log). The first awk counts the requests by method and code; the second gives the first and the last error.
Look at the 502s, from 13:24:32 to 13:24:44: about 12 seconds. There are so many POSTs because a 502 comes back in milliseconds. If none appears, look at before.log: without a new image, the script doesn’t deploy. 000 codes: curl can’t connect, check the name and the ca.crt in ~/loop.sh. POSTs with 400 or 429: the password in ~/.login.json isn’t your account’s.
Turn off Docker’s proxy
Section titled “Turn off Docker’s proxy”Once. First look at Docker’s configuration:
cat /etc/docker/daemon.jsonSince Phase 8, lesson 3, it has the log limits. tee replaces the whole file: write it with them and the new key. If the cat shows anything besides the limits, add it to the block before running it.
sudo tee /etc/docker/daemon.json <<'EOF'{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" }, "userland-proxy": false}EOFsudo systemctl restart dockerpgrep -a docker-proxyIf it didn’t exist, it creates it anyway. tee echoes the file back, and pgrep must print nothing. Restarting Docker stops and starts every container, once: our shop took about 13 seconds to be healthy again. If Docker doesn’t start, the JSON has an error: sudo journalctl -u docker -n 20 says which.
After: two copies that take turns
Section titled “After: two copies that take turns”On your computer, put your project at step 40, as in the introduction. The script and compose.production.yaml aren’t in the image: copy them to the server, with your user and its IP.
cd ~/backend-course/phase-3curl -O https://backenddesdecero.com/practicas/shop-api/40-zero-downtime.tar.gztar -xzf 40-zero-downtime.tar.gzcp -R 40-zero-downtime/. shop-api/rsync -av 40-zero-downtime/compose.production.yaml 40-zero-downtime/deployment ubuntu@192.168.5.15:/opt/shop/On Windows, everything in WSL except the copy to the VM, from PowerShell, with the step downloaded there (curl.exe -O and tar -xzf): scp -r instead of rsync -av. Then, from shop-api, commit and push, and wait for Publish. On the server, chmod +x deployment/*.sh, as in Phase 8. And measure:
2. The first deployment with two copies
cd /opt/shop(~/loop.sh 100 after.txt & sleep 5; deployment/deploy.sh > after.log 2>&1; wait)awk '{print $2, $3}' after.txt | sort | uniq -cWhat you will see:
691 GET 200 4904 POST 200Not a single 502. A lone one can be the rare race from Almost zero. Several: look in after.log for the docker-proxy warning and go over Turn off Docker’s proxy.
This is what our deployment wrote, with the time we added when measuring (trimmed, …):
13:26:06.014 deploy: new image: 8c210430516a -> ce248466fbf4 (the one running is kept as localhost:5000/shop-api:previous)13:26:06.288 time="2026-10-22T13:26:06+02:00" level=warning msg="Found orphan containers (shop-api-1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up."…13:26:09.910 deploy: api-a, with the new image (the other copy keeps answering)…13:26:30.927 deploy: the single-copy API leaves its port to api-b13:26:31.635 deploy: api-b, with the new image (api-a already answers with it)…13:26:43.795 deploy: done: api-a, api-b and the worker are running ce248466fbf4Our single copy was on 3001: it started with api-a. Found orphan containers is Compose warning about the single copy, whose service is no longer in the file: it’s expected.
Another title change, this time between two versions of step 40: 602 GETs and 4,210 POSTs, without an error.
Do two copies fit in 2 GiB?
Section titled “Do two copies fit in 2 GiB?”With everything running, a minute after a deployment:
3. The memory
free -mdocker stats --no-streamWhat you will see:
total used free shared buff/cache availableMem: 1949 1172 279 30 618 776Swap: 0 0 0NAME CPU % MEM USAGE / LIMIT MEM %shop-worker-1 0.71% 179.8MiB / 1.904GiB 9.22%shop-api-a-1 0.00% 124.3MiB / 1.904GiB 6.37%shop-api-b-1 0.09% 156.6MiB / 1.904GiB 8.03%shop-db-1 0.00% 44.56MiB / 1.904GiB 2.29%shop-cache-1 1.77% 7.512MiB / 1.904GiB 0.39%shop-redis-1 1.85% 9.684MiB / 1.904GiB 0.50%available: 776 MiB free, with no swap: they fit. We removed the line for our test registry, which your server doesn’t have. The copies grow with use: after about 4,100 logins, in an earlier run, they were at 249 and 219 MiB, with 720 MiB available.
When you’re done, turn the timer back on:
sudo systemctl start shop-deploy.timerrm ~/.login.json /opt/shop/before.* /opt/shop/after.*You have already seen it
Section titled “You have already seen it”- The “maintenance from 2:00 to 4:00” notices from some banks: they deploy with downtime, at the quietest hour.
- A new feature in an app that a friend already has and you don’t: often, a canary.
- Big websites that never close to update: many copies that take turns.
Common mistakes
Section titled “Common mistakes”- Leaving docker-proxy on. The GETs go through, but some POST gets a
502on every deployment. - Changing the second copy as soon as the first is healthy. nginx may still be setting it aside, and it runs out of copies.
- A shutdown longer than Docker’s wait. After 10 seconds,
SIGKILLcuts whatever is left. - Renaming a column in the same deployment as the code. The old copies fail as soon as the migration is applied.
- Fixing the POSTs with
proxy_next_upstream non_idempotent. nginx would repeat the ones that already arrived: one payment, twice.
Summary
Section titled “Summary”- The script changes one copy, waits for it to be healthy and 10 seconds more, and then the other.
- The copy that leaves stops accepting connections, finishes what it has half done and closes everything else at the end.
- nginx repeats a refused connection, even a POST, but not a POST already sent: that’s why docker-proxy goes.
- A rare race remains, closed by taking the copy out of the load balancer before stopping it.
- During the handover two versions run: migrations have to serve both.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- Module ngx_http_upstream_module, from nginx:
fail_timeout,downandweight. - proxy_next_upstream, from nginx.
- dockerd, from Docker:
daemon.jsonanduserland-proxy. - BlueGreenDeployment and CanaryRelease, by Martin Fowler.