Docker – Bulk Stop and Remove Containers in Bash

Had a bunch of annoying no-name containers I needed to remove because forgot to add the “–rm” flag on my “docker run” commands used for debugging containers.

Below is a snippet that works in git-bash and normal linux bash environments for bulk deletion of containers based on their name matching the grep pipe.

#This pattern targets and removes the weirdly named ones docker generates if no name is provided
#
#Test it first to ensure it targets the right containers!! - first command will echo all the ones that will be removed
docker ps -a | grep -v ":" | grep "_" | awk '{print $1}' | xargs echo
docker ps -a | grep -v ":" | grep "_" | awk '{print $1}' | xargs docker stop
docker ps -a | grep -v ":" | grep "_" | awk '{print $1}' | xargs docker rm -v

#This pattern targets and removes the ones I personally created in bulk that had "test_" in the name
docker ps -a | grep "test_" | awk '{print $1}' | xargs docker stop
docker ps -a | grep "test_" | awk '{print $1}' | xargs docker rm -v

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s