Last active
September 9, 2019 07:26
-
-
Save adidonato/230829fe157de1d96aed4c32601828b9 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/sh | |
command -v docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed. Aborting."; exit 1; } | |
read -p " | |
############################################################## | |
# WARNING! You are about to blow up all your docker stuff # | |
# By entering Y/y you will remove: # | |
# - all stopped containers # | |
# - all networks not used by at least one container # | |
# - all volumes not used by at least one container # | |
# - all dangling images # | |
# - all build cache # | |
############################################################## | |
Do you wish to continue? [Y/N] | |
" -n 1 -r | |
echo # (optional) move to a new line | |
# purge all unused stuff | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo -e "\033[1;32m Starting to purge all Docker containers, images, volumes & networks " | |
docker system prune -f || echo -e "\033[1;31m Failure system prune" | |
if [[ ! -z $(docker container ls -aq) ]] | |
then | |
# Stop all running containers | |
docker container stop $(docker container ls -aq) || echo -e "\033[1;31m Fail LS" | |
# Remove all stopped containers | |
docker container rm $(docker container ls -aq)|| echo -e "\033[1;31m Failed Removing Containers" | |
fi | |
if [[ ! -z $(docker image ls -aq) ]] | |
then | |
# Remove all images | |
docker image rm -f $(docker image ls -aq) || echo -e "\033[1;31m Failure removing images" | |
# Keep on pruning | |
fi | |
# remove all unsed images | |
docker image prune -a -f || echo -e "\033[1;31m Failure to image prune" | |
# prune all volume forcibly | |
docker volume prune -f || echo -e "\033[1;31m Failed pruning volumes" | |
# prune unused networks | |
docker network prune -f || echo -e "\033[1;31m Failed pruning net" | |
echo -e "\033[1;32m Done purging Docker stuff " | |
fi | |
if [[ $REPLY =~ ^[Nn]$ ]] | |
then | |
echo -e "\033[1;35m That was probably a wise choice, exiting.... " | |
fi | |
if [[ ! $REPLY =~ ^[YyNn]$ ]] | |
then | |
echo -e "\033[1;35m Nothing to do, exiting...." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment