docker reset script – docker prune

I’m using docker for a lot of projects and have to switch context very often – I know – that kind of sucks, but that’s how it is…

Most containers can’t be shared between projects and I don’t want to spend more resources than necessary as my machines are on their limits. So I wrote a small bash script to get rid of all running containers, images, … on my local machine:

#!/bin/bash
echo "###########################"
echo "# clean docker"
echo "###########################"

echo "# stopping containers"
docker stop `docker ps | awk '{print $1}' | grep -v CONTAINER` >> /dev/null 2>&1

echo "# prune system"
docker system prune -f --volumes >> /dev/null 2>&1

echo "###########################"
echo "# done"

It will output a text like this

###########################
# clean docker
###########################
# stopping containers
# prune system
###########################
# done

when executed.

After that there shouldn’t be any leftovers except your images. If you also want to get rid of those you can run:

docker system prune -f --all --volumes

Or just add –all in the script above.

Attention: This will delete all your local volumes and images. So make sure you don’t run this if any data in the containers is of importance for you.