#!/bin/sh # Equivalent of a rolling update for Docker Compose: replace one API replica # at a time, keeping the service at the configured replica count. Run # `docker compose build api` first when testing a new image. No volumes, # database rows or sessions are touched. set -eu ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) cd "$ROOT" COMPOSE="docker compose --env-file .env" COUNT=${API_REPLICAS:-2} wait_for_healthy() { deadline=$(( $(date +%s) + 90 )) while :; do ids=$($COMPOSE ps -q api) running=0 healthy=0 for id in $ids; do state=$(docker inspect --format '{{.State.Status}}' "$id") health=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$id") if [ "$state" = "running" ]; then running=$((running + 1)); fi if [ "$health" = "healthy" ] || [ "$health" = "none" ]; then healthy=$((healthy + 1)); fi done if [ "$running" -ge "$COUNT" ] && [ "$healthy" -ge "$COUNT" ]; then return 0 fi if [ "$(date +%s)" -ge "$deadline" ]; then echo "rolling-update: replicas did not become healthy" >&2 return 1 fi sleep 1 done } wait_for_healthy for id in $($COMPOSE ps -q api); do echo "replacing $id" docker stop --time 15 "$id" >/dev/null docker rm "$id" >/dev/null $COMPOSE up -d --no-deps --no-build --scale api="$COUNT" api wait_for_healthy done echo "rolling-update: all $COUNT replicas replaced"