Skip to content

Instantly share code, notes, and snippets.

@geekscape
Created June 18, 2024 05:25
Show Gist options
  • Save geekscape/403f056a3391bfee77bf074dd308843c to your computer and use it in GitHub Desktop.
Save geekscape/403f056a3391bfee77bf074dd308843c to your computer and use it in GitHub Desktop.
Bash script to remove Docker Images using a whitelist and keeping a specified number of copies ... beyond what "docker system prune" does
#!/bin/bash
#
# Usage
# ~~~~~
# docker_image_purge list
# docker_image_purge [ remove ]
#
# By default, this command only shows what Docker images it'll remove.
# Use the optional "remove" command line parameter to actually remove them !
#
# Internal variables that may be configured ...
# - DO_NOT_DELETE: Whitelist of Docker image repositories not to purge
# - MAXIMUM_SIMILAR_IMAGES: Number of images to preserve
#
# To Do
# ~~~~~
# - Command line parameter: Specific Docker image repository to clean-up
# - Pretty print Docker images, in order of grouped repository image size
LIST=${1:-DONT_LIST}
REMOVE=${1:-DONT_REMOVE}
MAXIMUM_SIMILAR_IMAGES=2
if [ "$LIST" == "list" ]; then
docker images | tr -s " " | sort | awk '{print $3,$7,$1,$2}' | column -c 80 -t
exit 0
fi
DO_NOT_DELETE=\
nvidia/cuda\|\
python\|\
tensorflow/tensorflow
free_disk_space() {
disk_partition=$1
df -h $disk_partition | tail -n +2 | {
read -r -a tokens
disk_space_available=${tokens[3]}
echo "Free space on " $disk_partition "is" $disk_space_available
}
}
echo Before: $(free_disk_space /home/users)
docker_images() {
docker images | tail -n +2
}
DOCKER_IMAGES=$(docker_images)
docker_images_count() {
cut -d" " -f1 | sort | uniq --count
}
sort_numeric() {
sort --numeric-sort --reverse
}
docker_image_count_list() {
echo "$DOCKER_IMAGES" | \
echo "$(docker_images_count)" | \
echo "$(sort_numeric)"
}
docker_image_ids_for_repository() {
desired_image_repository=$1
echo "$DOCKER_IMAGES" | while read -r line
do
echo "$line" | {
read -r -a tokens
docker_image_repository=${tokens[0]}
docker_image_id=${tokens[2]}
if [ $desired_image_repository == $docker_image_repository ]; then
echo $docker_image_id
fi
}
done
}
filtered_docker_image_ids() {
docker_image_count=$1
docker_image_repository=$2
echo $docker_image_repository | egrep -v $DO_NOT_DELETE >/dev/null
if [ $? -eq 0 ]; then
if [ $docker_image_count -gt $MAXIMUM_SIMILAR_IMAGES ]; then
echo "$DOCKER_IMAGES" | \
echo "$(docker_image_ids_for_repository $docker_image_repository)"
fi
fi
}
lines=$(docker_image_count_list)
echo "$lines" | while read -r line
do
echo "$line" | {
read -r -a tokens
docker_image_count=${tokens[0]}
docker_image_repository=${tokens[1]}
FILTERED_DOCKER_IMAGE_IDS=$(filtered_docker_image_ids $docker_image_count $docker_image_repository | tail -n +$(($MAXIMUM_SIMILAR_IMAGES + 1)) | uniq)
image_id_count=`echo "$FILTERED_DOCKER_IMAGE_IDS" | wc -w`
if [ $image_id_count -gt 0 ]; then
if [ "$REMOVE" == "remove" ]; then
action="Removing"
else
action="Could remove"
fi
echo $action $image_id_count $docker_image_repository "image(s)"
if [ "$REMOVE" == "remove" ]; then
docker rmi --force $FILTERED_DOCKER_IMAGE_IDS
fi
fi
}
done
echo After: $(free_disk_space /home/users)
if [ "$REMOVE" != "remove" ]; then
echo 'Add "remove" parameter to actually remove the Docker images'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment