Skip to content

Instantly share code, notes, and snippets.

@Santhin
Created January 13, 2021 22:51
Show Gist options
  • Save Santhin/bc976d32a41967d490d852f72367bb28 to your computer and use it in GitHub Desktop.
Save Santhin/bc976d32a41967d490d852f72367bb28 to your computer and use it in GitHub Desktop.
docker cleaner taken from stack/github
#!/bin/bash
# Stop and remove all containers
echo "Removing containers :"
if [ -n "$(docker container ls -aq)" ]; then
docker container stop $(docker container ls -aq);
docker container rm $(docker container ls -aq);
fi;
# Remove all images
echo "Removing images :"
if [ -n "$(docker images -aq)" ]; then
docker rmi -f $(docker images -aq);
fi;
# Remove all volumes
echo "Removing volumes :"
if [ -n "$(docker volume ls -q)" ]; then
docker volume rm $(docker volume ls -q);
fi;
# Remove all networks
echo "Removing networks :"
# Skip default networks : bridge, host, none
if [ -n "$(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}')" ]; then
docker network rm $(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}');
fi;
# Your installation should now be all fresh and clean.
# The following commands should not output any items:
# docker ps -a
# docker images -a
# docker volume ls
# The following command show only show the default networks:
# docker network ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment