Files
ansible-role-gitea/files/docker-prune.sh
claudeandClaude Opus 5.5 4d7b0b9e01
Demo / hello (push) Successful in 25s
Runner: prune Docker data in DinD on a schedule
Add a docker-prune native sidecar (docker:29.5.2-cli, matching the
deployed DinD) that runs files/docker-prune.sh against DinD's socket
every 15 minutes. It removes stopped containers, unused volumes,
dangling images and build cache older than 6 hours, and once Docker
uses more than 30 GB it removes every unused image except the pinned
job images. Running containers and anything a container uses are
never touched. Settings are gitea_actions_prune_* defaults.

Refs DevOps/software-infrastructure#38

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:24:55 -05:00

102 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
# Prunes Docker data in the runner's Docker-in-Docker so the docker-data
# emptyDir stays below its sizeLimit. Runs as a sidecar next to DinD.
#
# Never touches anything a job is using:
# - only stopped containers are pruned (Docker never prunes running ones);
# - volumes are removed only when no container references them, and Docker
# refuses to remove a volume that is in use;
# - images are removed with a plain `docker image rm`, which fails for any
# image a container (running or stopped) uses, and images in KEEP are skipped.
#
# Settings (environment):
# PRUNE_INTERVAL_SECONDS seconds between passes [900]
# PRUNE_MAX_AGE_MINUTES age before stopped containers, unused
# volumes, dangling images and build
# cache are removed [360]
# PRUNE_IMAGES_ABOVE_GB when Docker uses more than this, remove
# every unused image not in KEEP [30]
# PRUNE_KEEP_IMAGES space-separated image refs never removed,
# normally the runner's job images []
# PRUNE_ONCE set to 1 to run one pass and exit (tests)
set -u
interval=${PRUNE_INTERVAL_SECONDS:-900}
max_age=${PRUNE_MAX_AGE_MINUTES:-360}
images_above_gb=${PRUNE_IMAGES_ABOVE_GB:-30}
keep=${PRUNE_KEEP_IMAGES:-}
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) docker-prune: $*"; }
# Bytes Docker uses for images, containers, volumes and build cache.
docker_bytes() {
docker system df --format '{{.Size}}' | awk '
function bytes(s, n, u) {
n = s + 0; u = s; sub(/^[0-9.]+/, "", u)
if (u == "kB" || u == "KB") return n * 1000
if (u == "MB") return n * 1000 ^ 2
if (u == "GB") return n * 1000 ^ 3
if (u == "TB") return n * 1000 ^ 4
return n
}
{ total += bytes($1) }
END { printf "%.0f\n", total }'
}
gb() { awk -v b="$1" 'BEGIN { printf "%.1f", b / 1000 ^ 3 }'; }
is_kept() {
for k in $keep; do
[ "$1" = "$k" ] && return 0
done
return 1
}
prune_volumes() {
now=$(date -u +%s)
docker volume ls -q --filter dangling=true | while read -r v; do
created=$(docker volume inspect -f '{{.CreatedAt}}' "$v" 2>/dev/null) || continue
# CreatedAt is RFC 3339; keep the date and time to the second.
created=$(date -u -D '%Y-%m-%dT%H:%M:%S' -d "$(echo "$created" | cut -c1-19)" +%s 2>/dev/null) || continue
if [ $((now - created)) -ge $((max_age * 60)) ]; then
docker volume rm "$v" >/dev/null 2>&1 && log "removed volume $v"
fi
done
}
prune_images_over_threshold() {
used=$(docker_bytes)
limit=$(awk -v g="$images_above_gb" 'BEGIN { printf "%.0f\n", g * 1000 ^ 3 }')
[ "$used" -gt "$limit" ] || return 0
log "using $(gb "$used") GB, above ${images_above_gb} GB: removing unused images"
docker image ls --format '{{.ID}} {{.Repository}}:{{.Tag}}' | while read -r id ref; do
is_kept "$ref" && continue
# Fails, and is skipped, for any image a container uses.
docker image rm "$ref" >/dev/null 2>&1 && log "removed image $ref" && continue
case $ref in *'<none>'*) docker image rm "$id" >/dev/null 2>&1 && log "removed image $id" ;; esac
done
}
pass() {
before=$(docker_bytes)
docker container prune -f --filter "until=${max_age}m" >/dev/null
prune_volumes
docker image prune -f --filter "until=${max_age}m" >/dev/null
docker builder prune -f --filter "until=${max_age}m" >/dev/null 2>&1
prune_images_over_threshold
after=$(docker_bytes)
log "pass done: $(gb "$before") GB -> $(gb "$after") GB"
}
until docker info >/dev/null 2>&1; do
log "waiting for Docker"
sleep 5
done
log "started: every ${interval}s, max age ${max_age}m, images above ${images_above_gb} GB, keep: ${keep:-none}"
while :; do
pass
[ "${PRUNE_ONCE:-0}" = 1 ] && exit 0
sleep "$interval"
done