Skip to content

Instantly share code, notes, and snippets.

@aravindkumarsvg
Created July 14, 2017 16:04
Show Gist options
  • Save aravindkumarsvg/2733a5413b15149993fcaaf2f130d5e1 to your computer and use it in GitHub Desktop.
Save aravindkumarsvg/2733a5413b15149993fcaaf2f130d5e1 to your computer and use it in GitHub Desktop.
Removes the Docker images and also able to add some exclusions for image deletion
#!/bin/bash
#================================================
# Removes the Docker images based on conditions
#================================================
# Variable Declaration
declare -A image_exclusion=( ["node"]="8-alpine" ["jenkins"]="latest" )
# Checks whether image has been excluded or not
imageExclusionChecker() {
if [[ ! ( ! -z ${image_exclusion[$1]} && ${image_exclusion[$1]} == $2 ) ]]
then
return 1
else
return 0
fi
}
# Removes the Docker image
removeImages() {
local iteration=0
while read name tag imageid rest
do
# Skips the first iteration, since it contains only headers
if [ $iteration == 0 ]
then
iteration=` expr $iteration + 1 `
continue
fi
# Checks whether the image has been excluded from deletion
imageExclusionChecker $name $tag
if [ $? == 1 ]
then
docker rmi -f $imageid
else
continue
fi
done << EOF
$(docker images -a)
EOF
}
# Main flow of execution
main() {
# Removes the Docker iamges
removeImages
}
# Starts the Execution
main
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment