Pulizia: dice quali nomi image prune -a toglie alle immagini in uso

Docker non toglie un'immagine che un contenitore usa, ma con -a le toglie
i nomi in più: con lo store classico tutti tranne l'ultimo in ordine
alfabetico, anche quello da cui il contenitore è stato creato
(un'applicazione di Unraid creata da app:1.2 che si chiama anche
app:latest resta senza app:1.2); con containerd tutti tranne quelli dei
contenitori. La prova saltava ogni immagine in uso e i documenti la
davano per intoccata (rilievo della revisione, controllato sul codice di
moby v24, v27 e master).

La pulizia vera confronta i nomi prima e dopo la potatura e scrive, nome
per nome, quelli tolti a un'immagine che resta; la prova li stima secondo
lo store e li conta, e le prove controllano che stima e pulizia vera
coincidano con l'uno e con l'altro store. Il comando resta quello
chiesto, docker image prune -a -f --filter until=24h; docs/processo.md e
docs/docker.md dicono che cosa succede ai nomi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-21 11:56:41 +02:00
1 parent e24ebd24f0
commit 68100b0295
7 files changed
+233 -40

No files matched your search

@@ -276,6 +276,16 @@ public sealed partial class PipelineTests
CollectionAssert.AreEquivalent(SharedDaemon.VolumesLeft, run.Volumes, run.Report);
CollectionAssert.AreEquivalent(SharedDaemon.NetworksLeft, run.Networks, run.Report);
// An image a container uses stays, but «image prune -a» takes its extra names: with the classic store all but
// the last in the order of their full names, even the one the container was made from (Jellyfin, from :10.9).
// The log says so, name by name.
StringAssert.Contains(run.Output, "== Store delle immagini del demone: classico", run.Report);
CollectionAssert.Contains(run.Images, "img-rilascio|192.168.30.23:3000/alby96/febe:latest", run.Report);
CollectionAssert.Contains(run.Images, "img-jellyfin|jellyfin/jellyfin:latest", run.Report);
StringAssert.Contains(run.Output, " tolto il nome jellyfin/jellyfin:10.9; l'immagine img-jellyfin resta\n", run.Report);
StringAssert.Contains(run.Output, " tolto il nome 192.168.30.23:3000/alby96/febe:0.1.0; l'immagine img-rilascio resta\n", run.Report);
Assert.AreEqual("2", SummaryCount(run.Output, "nomi tolti a immagini che restano"), run.Report);
// Not even an attempt on what is not the CI's (Docker would refuse some of it: the script must not ask), and
// the prunes of everyone only with their filter, once.
foreach (var call in run.Calls)
@@ -341,6 +351,8 @@ public sealed partial class PipelineTests
" toglierei 192.168.30.23:3000/alby96/febe:0.0.9 (img-febe-vec) · 400MB · creata 34 giorni fa\n",
" toglierei node:24-trixie (img-node) · 410MB · creata 13 giorni fa\n",
" toglierei un'immagine senza nome (img-plex-pre) · 300MB · creata 69 giorni fa\n",
" toglierei il nome 192.168.30.23:3000/alby96/febe:0.1.0; l'immagine img-rilascio resta, perché la usa un contenitore\n",
" toglierei il nome jellyfin/jellyfin:10.9; l'immagine img-jellyfin resta, perché la usa un contenitore\n",
"la cache di costruzione del demone occupa 5.3GB",
" toglierei sha-4e5f6a7 (pubblicata il 2026-10-05T01:00:00+02:00)\n",
$" {UntaggedDigests.Length} versioni senza etichetta (sha256:…",
@@ -350,7 +362,7 @@ public sealed partial class PipelineTests
StringAssert.Contains(tried.Output, expected, tried.Report);
}
foreach (var kept in new[] { "plexinc/pms-docker", "encelado:latest", "mimante:latest", "runner-images", "moby/buildkit", "act-toolcache", "encelado-dati", "febe:latest", "toglierei main", "toglierei latest", "toglierei 0.1.", "GITEA-ACTIONS-TASK-90", "GITEA-ACTIONS-TASK-60", "GITEA-ACTIONS-TASK-88", "GITEA-ACTIONS-TASK-89", "GITEA-ACTIONS-TASK-77" })
foreach (var kept in new[] { "plexinc/pms-docker", "encelado:latest", "mimante:latest", "runner-images", "moby/buildkit", "act-toolcache", "encelado-dati", "febe:latest", "jellyfin/jellyfin:latest", "toglierei main", "toglierei latest", "toglierei 0.1.", "GITEA-ACTIONS-TASK-90", "GITEA-ACTIONS-TASK-60", "GITEA-ACTIONS-TASK-88", "GITEA-ACTIONS-TASK-89", "GITEA-ACTIONS-TASK-77" })
{
Assert.DoesNotContain(kept, tried.Output, tried.Report);
}
@@ -358,7 +370,7 @@ public sealed partial class PipelineTests
// And it counts what the real cleanup then takes.
var real = await RunCleanupAsync(bash, world, "completa");
Assert.AreEqual(0, real.Code, real.Report);
foreach (var kind in new[] { "contenitori di prova della CI di Febe", "immagini febe-ci", "builder buildx della CI di Febe", "contenitori fermi di act_runner", "volumi di act_runner", "reti di act_runner", "immagini che nessun contenitore usa", "etichette sha- sul registro" })
foreach (var kind in new[] { "contenitori di prova della CI di Febe", "immagini febe-ci", "builder buildx della CI di Febe", "contenitori fermi di act_runner", "volumi di act_runner", "reti di act_runner", "immagini che nessun contenitore usa", "nomi tolti a immagini che restano", "etichette sha- sul registro" })
{
Assert.AreEqual(SummaryCount(real.Output, kind), SummaryCount(tried.Output, kind), $"{kind}\n{tried.Report}\n{real.Report}");
}
@@ -371,11 +383,28 @@ public sealed partial class PipelineTests
// With the containerd image store «until» counts from when a name reached the daemon, which no command shows;
// the dry run can only give the age of the image and must say so.
var bash = RequireBash();
var tried = await RunCleanupAsync(bash, SharedDaemon.World with { Store = "containerd" }, "completa", "--prova");
var world = SharedDaemon.World with { Store = "containerd" };
var tried = await RunCleanupAsync(bash, world, "completa", "--prova");
Assert.AreEqual(0, tried.Code, tried.Report);
StringAssert.Contains(tried.Output, "== Store delle immagini del demone: containerd (le 24 ore contano da quando un nome è arrivato sul demone)\n", tried.Report);
StringAssert.Contains(tried.Output, "l'elenco è solo indicativo)", tried.Report);
CollectionAssert.Contains(tried.Calls, "BUILDX_BUILDER=default docker info --format {{json .DriverStatus}}", "the store is read, never changed");
// Of an image a container uses, containerd keeps the names the containers were made from and takes the others:
// Jellyfin keeps :10.9 and loses :latest, the other way round from the classic store. The dry run says so, and
// the real cleanup does it.
StringAssert.Contains(tried.Output, " toglierei il nome jellyfin/jellyfin:latest; l'immagine img-jellyfin resta, perché la usa un contenitore\n", tried.Report);
StringAssert.Contains(tried.Output, " toglierei il nome 192.168.30.23:3000/alby96/febe:0.1.0; l'immagine img-rilascio resta, perché la usa un contenitore\n", tried.Report);
Assert.DoesNotContain("jellyfin/jellyfin:10.9", tried.Output, tried.Report);
var real = await RunCleanupAsync(bash, world, "completa");
Assert.AreEqual(0, real.Code, real.Report);
CollectionAssert.AreEquivalent(SharedDaemon.ImagesLeft, real.ImageIds, real.Report);
CollectionAssert.Contains(real.Images, "img-jellyfin|jellyfin/jellyfin:10.9", real.Report);
CollectionAssert.Contains(real.Images, "img-rilascio|192.168.30.23:3000/alby96/febe:latest", real.Report);
foreach (var kind in new[] { "immagini che nessun contenitore usa", "nomi tolti a immagini che restano" })
{
Assert.AreEqual(SummaryCount(real.Output, kind), SummaryCount(tried.Output, kind), $"{kind}\n{tried.Report}\n{real.Report}");
}
}
[TestMethod]
@@ -552,7 +581,8 @@ public sealed partial class PipelineTests
/// A daemon as the runner's is likely to be (format in fake-docker.sh): a real Febe and Unraid apps, running and
/// stopped; the job container of the cleanup itself; leftovers of act_runner, with the names of today's act_runner
/// («_WORKFLOW-…») and of older ones («-WORKFLOW-…» with a hash), and look-alikes; leftovers of the CI of Febe;
/// images used and unused, named and dangling, old and young; volumes and networks of every kind.
/// images used and unused, named and dangling, old and young; volumes and networks of every kind. The images of the
/// real Febe and of Jellyfin have two names each, and Jellyfin was created from the one that is not the last.
/// </summary>
private static class SharedDaemon
{
@@ -564,6 +594,7 @@ public sealed partial class PipelineTests
[
"c-febe-vero|febe|running|43200|192.168.30.23:3000/alby96/febe:latest|img-rilascio|febe.ci.workflow=rilascio,febe.ci.run=777,febe.ci.ref=v0.1.0|febe-dati|bridge",
"c-plex|plex|exited|90000|plexinc/pms-docker:latest|img-plex||plex-config|br0",
"c-jellyfin|jellyfin|running|90000|jellyfin/jellyfin:10.9|img-jellyfin||",
"c-encelado|encelado|exited|5000|encelado:latest|img-encelado|||encelado_default",
$"c-lavoro|{Job90}|running|5|docker.gitea.com/runner-images:ubuntu-latest|img-runner||{Job90},{Job90}-env,act-toolcache|{Job90}-docker-network",
$"c-act-fermo|{Job53}|exited|3000|node:24-trixie|img-node||{Job53},{Job53}-env,act-toolcache|{Job53}-prove-network",
@@ -584,6 +615,7 @@ public sealed partial class PipelineTests
"img-febe-vecchia|192.168.30.23:3000/alby96/febe:0.0.9|50000||400MB",
"img-plex|plexinc/pms-docker:latest|100000||300MB",
"img-plex-precedente||100000||300MB",
"img-jellyfin|jellyfin/jellyfin:10.9,jellyfin/jellyfin:latest|100000||900MB",
"img-encelado|encelado:latest|5000||250MB",
"img-encelado-dev|encelado:dev|3000||250MB",
"img-mimante-nuova|mimante:latest|60||180MB",
+57 -4
View File
@@ -8,7 +8,8 @@
# - «image rm <name>» only untags when the image has other names, and refuses an image a container uses (always if the
# container runs, without -f if it is stopped);
# - «image prune» takes only images no container uses, running or stopped: without -a only those without a name, with
# -a any; «until» keeps those created in the last 72 or 24 hours;
# -a any; «until» keeps those created in the last 72 or 24 hours. With -a an image a container uses stays but loses
# its extra names, as moby does it with the classic store or, with $FAKE_DOCKER_WORLD/store, with containerd;
# - «builder prune» only answers (the cache is not modelled);
# - «volume rm» refuses a volume a container mounts, running or stopped, even with -f;
# - «network rm» refuses the predefined networks and a network a running container is attached to;
@@ -207,6 +208,7 @@ container_inspect() {
"{{.Created}}") created "$age" ;;
"{{.Config.Image}}") echo "$config" ;;
"{{.Image}}") echo "$image_id" ;;
"{{.Image}} {{.Config.Image}}") echo "$image_id $config" ;;
"{{.Name}}") echo "/$name" ;;
'{{index .Config.Labels "febe.ci.prova"}}') label_value "$labels" febe.ci.prova "<no value>" ;;
*) unexpected "container inspect -f $format" ;;
@@ -313,6 +315,43 @@ image_rm() {
echo "Deleted: $id"
}
# The full name of an image name, the way Docker sorts references: docker.io/library/ or docker.io/ before the names
# of Docker Hub written without a domain, :latest when the tag is missing.
full_name() {
local name="$1" first
[[ "${name##*/}" == *:* ]] || name="$name:latest"
if [[ "$name" != */* ]]; then
echo "docker.io/library/$name"
return
fi
first="${name%%/*}"
if [[ "$first" == *.* || "$first" == *:* || "$first" == localhost ]]; then echo "$name"; else echo "docker.io/$name"; fi
}
# The names «image prune -a» leaves to an image a container uses, comma separated, the way moby does it. With the
# classic store it removes the references in lexical order of their full names and only the last one is refused
# (ImageDelete keeps the single reference of an image in use); with containerd it keeps the names the containers were
# created from and, when a container uses the image by id and no name would be left, the last one in lexical order.
names_kept() {
local image="$1" names="$2" sorted name full cid cname state age config image_id rest by_id=0
local -A from=()
local kept=()
sorted=$(IFS=','; for name in $names; do printf '%s %s\n' "$(full_name "$name")" "$name"; done | LC_ALL=C sort)
if [[ "$(store)" == classic ]]; then
tail -n 1 <<< "$sorted" | cut -d' ' -f2-
return
fi
while IFS='|' read -r cid cname state age config image_id rest; do
[[ -n "$cid" && "$image_id" == "$image" ]] || continue
if [[ "$config" == "$image" ]]; then by_id=1; else from[$(full_name "$config")]=1; fi
done < "$world/containers"
while read -r full name; do
[[ -n "${from[$full]:-}" ]] && kept+=("$name")
done <<< "$sorted"
((${#kept[@]} > 0 || !by_id)) || kept=("$(tail -n 1 <<< "$sorted" | cut -d' ' -f2-)")
(IFS=','; echo "${kept[*]}")
}
image_prune() {
local all=0 hours=0
case "$*" in
@@ -322,14 +361,28 @@ image_prune() {
*) unexpected "image prune $*" ;;
esac
broken "image prune" && { echo "Error response from daemon: a prune operation is already running" >&2; exit 1; }
local id names age labels size gone=() tag
local id names age labels size gone=() untag=() tag left
while IFS='|' read -r id names age labels size; do
[[ -n "$id" ]] || continue
((all)) || [[ -z "$names" ]] || continue
((age >= hours * 60)) || continue
used_by "$id" any || gone+=("$id")
if used_by "$id" any; then
[[ "$names" == *,* ]] && untag+=("$id")
else
gone+=("$id")
fi
done < "$world/images"
((${#gone[@]} == 0)) || echo "Deleted Images:"
((${#gone[@]} + ${#untag[@]} == 0)) || echo "Deleted Images:"
for id in "${untag[@]}"; do
IFS='|' read -r id names age labels size <<< "$(image_line "$id")"
left=$(names_kept "$id" "$names")
IFS=',' read -r -a tags <<< "$names"
for tag in "${tags[@]}"; do
[[ ",$left," == *",$tag,"* ]] || echo "untagged: $tag"
done
drop "$world/images" "$id"
printf '%s|%s|%s|%s|%s\n' "$id" "$left" "$age" "$labels" "$size" >> "$world/images"
done
for id in "${gone[@]}"; do
IFS='|' read -r id names age labels size <<< "$(image_line "$id")"
tags=()