Skip to content

Instantly share code, notes, and snippets.

@headStyleColorRed
Last active January 24, 2021 10:08
Show Gist options
  • Save headStyleColorRed/9fe42413a2c530f21a307439c4e6c7b6 to your computer and use it in GitHub Desktop.
Save headStyleColorRed/9fe42413a2c530f21a307439c4e6c7b6 to your computer and use it in GitHub Desktop.
# Discern if there are images and containers
docker ps -aq > containers.txt
docker images -q > images.txt
docker volume ls -q > volumes.txt
ARE_THERE_CONTAINERS=$(cat containers.txt | wc -l)
ARE_THERE_IMAGES=$(cat images.txt | wc -l)
ARE_THERE_VOLUMES=$(cat volumes.txt | wc -l)
if [ $ARE_THERE_CONTAINERS -gt 0 ]; then
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)
else
echo "There are no available containers"
fi
if [ $ARE_THERE_IMAGES -gt 0 ]; then
docker rmi -f $(docker images -q)
else
echo "There are no available images"
fi
if [ $ARE_THERE_VOLUMES -gt 0 ]; then
docker volume rm $(docker volume ls -q)
else
echo "There are no available volumes"
fi
rm containers.txt images.txt volumes.txt
echo "Docker system cleaned succesfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment