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

+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=()