Run automatic Docker image cleanup after build and update (#1232)

This commit is contained in:
Leendert de Borst
2025-09-17 19:47:43 +02:00
committed by Leendert de Borst
parent c2f2511f6a
commit b6d3f9e70f

View File

@@ -1488,6 +1488,84 @@ print_success_box() {
printf "${MAGENTA}%s${NC}\n" "$footer"
}
# Function to clean up old Docker images
cleanup_docker_images() {
printf "\n${YELLOW}+++ Cleaning up old Docker images +++${NC}\n"
# Only clean up AliasVault-related images and dangling images
local current_version="$1"
local removed_count=0
local total_count=0
if [ "$VERBOSE" = true ]; then
printf "${CYAN} Removing old AliasVault images...${NC}\n"
# Remove dangling images (untagged)
docker image prune -f
# Get all images containing "aliasvault" (case-insensitive) in the repository name
docker images --format "{{.Repository}}:{{.Tag}}" | grep -i "aliasvault" | \
while read -r image_full; do
# Extract just the tag from the full image name
local tag="${image_full##*:}"
# Skip if image is currently being used by a container
if ! docker ps -a --format "{{.Image}}" | grep -q "^${image_full}$"; then
# Skip if it's the current version or latest
if [ "$tag" != "$current_version" ] && [ "$tag" != "latest" ]; then
((total_count++))
printf " Removing old image: $image_full"
# Remove by full repository:tag name, not by ID
if docker rmi "$image_full" > /dev/null 2>&1; then
printf " ${GREEN}${NC}\n"
((removed_count++))
else
printf " ${YELLOW}(skipped - may share layers)${NC}\n"
fi
fi
fi
done
if [ $total_count -eq 0 ]; then
printf " ${CYAN}No old images to remove${NC}\n"
elif [ $removed_count -eq $total_count ]; then
printf " ${GREEN}Successfully removed all $removed_count old image(s)${NC}\n"
else
printf " ${GREEN}Removed $removed_count of $total_count old image(s)${NC}\n"
fi
printf "${GREEN}✓ Docker image cleanup completed${NC}\n"
else
(
# Silent cleanup with spinner
{
# Remove dangling images
docker image prune -f > /dev/null 2>&1
# Remove old AliasVault images by full name
docker images --format "{{.Repository}}:{{.Tag}}" | grep -i "aliasvault" | \
while read -r image_full; do
# Extract just the tag from the full image name
local tag="${image_full##*:}"
# Skip if image is currently being used by a container
if ! docker ps -a --format "{{.Image}}" | grep -q "^${image_full}$"; then
# Skip if it's the current version or latest
if [ "$tag" != "$current_version" ] && [ "$tag" != "latest" ]; then
# Remove by full repository:tag name
docker rmi "$image_full" > /dev/null 2>&1 || true
fi
fi
done
} &
CLEANUP_PID=$!
show_spinner $CLEANUP_PID "Cleaning up old Docker images "
wait $CLEANUP_PID
)
printf "${GREEN}✓ Docker image cleanup completed${NC}\n"
fi
}
# Function to print success message
print_install_success_message() {
printf "\n"
@@ -1825,6 +1903,9 @@ handle_build() {
exit 1
fi
# Clean up old Docker images
cleanup_docker_images "$target_version"
# Only show success message if we made it here without errors and health check passed
print_install_success_message
}
@@ -2594,6 +2675,9 @@ handle_install_version() {
exit 1
fi
# Clean up old Docker images
cleanup_docker_images "$target_version"
# Only show success message if we made it here without errors and health check passed
print_install_success_message
}