Created
July 17, 2018 13:56
-
-
Save r0yfire/1de555a20e226cea5b3289ca14e50350 to your computer and use it in GitHub Desktop.
Docker Cleanup Script
This file contains 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 | |
# | |
# Cleanup Docker | |
# | |
if [ ! $(command -v docker) ]; then | |
echo "Docker is not installed." | |
exit 1 | |
fi | |
disk_space_before=$(df -k / | awk 'NR==2 {print $4}') | |
docker volume rm $(docker volume ls -qf dangling=true) 2> /dev/null | |
docker rmi $(docker images -f "dangling=true" -q) 2> /dev/null | |
VERSION=$(docker --version | cut -d' ' -f3 | cut -d',' -f1) | |
if [ $VERSION > 1.9 ]; then | |
docker volume rm $(docker volume ls -qf dangling=true) 2> /dev/null | |
else | |
docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes 2> /dev/null | |
fi | |
disk_space_after=$(df -k / | awk 'NR==2 {print $4}') | |
freed_diskspace=$(($disk_space_after - $disk_space_before)) | |
echo -e "\nDocker cleanup finished. Total disk space freed: $freed_diskspace Kb." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment