Created
March 6, 2018 07:32
-
-
Save lazyfrosch/030c10c444ff49aba86c62fac3c13cbb to your computer and use it in GitHub Desktop.
Docker Cleanup for dangling images and volumes, but keep named volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RC=0 | |
IMAGES=($(docker images -q -f dangling=true)) | |
VOLUMES=($(docker volume ls -q -f dangling=true)) | |
if [ "${#IMAGES[@]}" -ne 0 ]; then | |
for image in "${IMAGES[@]}" | |
do | |
echo "Deleting dangling image $image ..." | |
docker rmi $image | |
if [ $? -ne 0 ]; then | |
RC=1 | |
fi | |
done | |
fi | |
if [ "${#VOLUMES[@]}" -ne 0 ]; then | |
for volume in "${VOLUMES[@]}" | |
do | |
if echo "$volume" | grep -qP "[a-f0-9]{64}"; then | |
echo "Deleting dangling volume $volume ..." | |
docker volume rm "$volume" | |
if [ $? -ne 0 ]; then | |
RC=1 | |
fi | |
else | |
echo "NOT cleaning named volume: $volume" | |
fi | |
done | |
fi | |
exit $RC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment