Skip to content

Instantly share code, notes, and snippets.

@dcchambers
Forked from n3r0-ch/docker-nuke
Last active December 6, 2019 19:58
Show Gist options
  • Save dcchambers/0b3d0f927f27bf8339e3367b8b734dae to your computer and use it in GitHub Desktop.
Save dcchambers/0b3d0f927f27bf8339e3367b8b734dae to your computer and use it in GitHub Desktop.
Docker Nuke (Safely) πŸ³πŸ’£- Clean up your Docker environment in a (sort of) safe way!
#!/bin/bash
#Check if user is root
if [ $UID != 0 ]; then
echo "You need to be root to use this script."
exit 1
fi
echo "docker-nuke-safely exists to do one thing; clean up your Docker environment. It's not called docker-carefully-and-nicely-spritz-up. Be careful!"
echo
read -p "Nuke now? [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
read -p "Would you like to stop all containers? [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Stoping all containers"
docker stop $(docker ps -a -q)
fi
echo
read -p "Would you like to delete all containers? [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Deleting all containers"
docker rm $(docker ps -a -q)
fi
echo
read -p "Would you like to delete all images? [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Deleting all images"
docker rmi $(docker images -q)
fi
echo
read -p "Would you like to delete all volumes? [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Deleting all volumes"
rm -rf /var/lib/docker/volumes/*
rm -rf /var/lib/docker/vfs/dir/*
fi
echo
echo
echo "Finished nuking"
else
echo "Cancelled"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment