Skip to content

Instantly share code, notes, and snippets.

@lazyfrosch
Created March 6, 2018 07:32
Show Gist options
  • Save lazyfrosch/030c10c444ff49aba86c62fac3c13cbb to your computer and use it in GitHub Desktop.
Save lazyfrosch/030c10c444ff49aba86c62fac3c13cbb to your computer and use it in GitHub Desktop.
Docker Cleanup for dangling images and volumes, but keep named volumes
#!/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