Skip to content

Instantly share code, notes, and snippets.

@r0yfire
Created July 17, 2018 13:56
Show Gist options
  • Save r0yfire/1de555a20e226cea5b3289ca14e50350 to your computer and use it in GitHub Desktop.
Save r0yfire/1de555a20e226cea5b3289ca14e50350 to your computer and use it in GitHub Desktop.
Docker Cleanup Script
#!/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