Shell Script To Cleanup <none> and Dangling docker images after Jenkins Build


(Lisa Mathew) #1

The following shell script will delete all the <none> and Dangling images from your Jenkins docker build box.

The script will initially check for any existing none or dangling images. If it exists, it will execute the removal script based on if condition.

#!/bin/bash
#removing <none> images
none_images=`docker images | grep "^<none>" | awk '{ print $3 }'`
if [ ! -z $none_images ]
then
docker rmi $(docker images | grep "^<none>" | awk '{ print $3 }')
else
echo "No <none> images found for cleanup"
fi
#removing Dangling images
dangling_images=`sudo docker images -f "dangling=true" -q`
if [ ! -z $dangling_images  ]
then
   docker rmi $(docker images -f "dangling=true" -q)
else
   echo "No Dangling images found for cleanup"
fi
echo "Docker Images cleanup Done"

If you have more ideas, please send a reply to add up to the existing script.