#!/bin/sh set -eu # Safe cleanup for the Coolify Docker destination. # Removes old app images that are not used by running containers. # Volumes are intentionally never pruned: they hold Postgres/app data. KEEP_PER_REPO="${KEEP_PER_REPO:-1}" CONTAINER_UNTIL="${CONTAINER_UNTIL:-24h}" BUILDER_UNTIL="${BUILDER_UNTIL:-24h}" if [ "$#" -gt 0 ]; then REPOS="$*" else REPOS="n6cnr60anmruiwkiey0pukye korokrhpoyqxf0nw2y8vk7lp" fi used_images="$(docker ps --format '{{.Image}}' | sort -u)" for repo in $REPOS; do count=0 docker image ls "$repo" --format '{{.Repository}}:{{.Tag}} {{.ID}}' | while read -r image image_id; do [ -n "$image" ] || continue if printf '%s\n' "$used_images" | grep -qx "$image"; then count=$((count + 1)) echo "keep running image: $image" continue fi count=$((count + 1)) if [ "$count" -le "$KEEP_PER_REPO" ]; then echo "keep recent image: $image" continue fi echo "remove old image: $image" docker rmi "$image_id" || true done done docker container prune -f --filter "until=$CONTAINER_UNTIL" docker image prune -f docker builder prune -f --filter "until=$BUILDER_UNTIL"