Files
deploy/remote/remove_replaced_images.sh
T
Christian Manivong abefafe070
CI / check (pull_request) Successful in 22s
fix: record the replaced images before the database step, for the whole project
The record was taken right before the engine's swap. Since then the database
step and the broker replace their containers earlier, so a new PostgreSQL or
Valkey image left the old one on the host for good; docker image prune -af
used to take it. It also listed only the engine services, so postgres (behind
its compose profile), apt-cacher-ng, signal-api and the retired registry were
never recorded.

deploy.sh now records the images of every container of the compose project
netork, by its label, before the database step, and keeps only image IDs:
they are expanded into the command that removes them. They are still removed
only after the verification and the migration; a failed deploy removes
nothing. remove_replaced_images.sh says nothing about an image still in use,
which most of them now are on every deploy, and names only what it removed or
could not remove.
2026-12-02 17:27:03 +01:00

31 lines
1.4 KiB
Bash

#!/bin/bash
# remove_replaced_images.sh <image id>...: the images a deploy replaced, and
# nothing else. deploy.sh records the IDs of every container of the compose
# project netork before the first of them can be replaced, and runs this on the
# host once the swap is verified and the migration ran.
#
# It used to be `docker image prune -af`, which removes every image no container
# uses at that moment. On a host that runs other things too, that took their
# rollback images (NetOrk/kb-server#157). An ID goes here only if no container --
# netOrk's or anyone else's -- uses it: a redeploy of the same tag, or a database
# whose image did not change, replaces nothing and keeps it without a word. It
# goes by every tag it carries, or by its ID once a moving tag such as latest-dev
# has left it without one. Only what was removed, or could not be, is reported.
set -u
in_use=$(docker ps -aq | xargs -r docker inspect --format '{{.Image}}' 2>/dev/null | sort -u)
for id in "$@"; do
if printf '%s\n' "$in_use" | grep -qx -- "$id"; then
continue
fi
tags=$(docker image inspect --format '{{range .RepoTags}}{{.}} {{end}}' "$id" 2>/dev/null) || continue
# shellcheck disable=SC2086 # the tags are separate words on purpose
if docker image rm -- ${tags:-$id} >/dev/null 2>&1; then
echo "removed ${tags:-$id}"
else
echo "kept ${id:7:12}: docker would not remove it"
fi
done
exit 0