Some docker helper commands

So a lot of times when you’re working with some cool docker containers, you might need to remove all those dead containers that are just lying around. This could happen if you’re running docker containers in background or forgot to use --rm flag.

Not to worry, just run this command to remove ALL running docker containers in one go:

That will list all docker containers, cut the first column, remove the one with CONTAINER in it and pipe all the remaining ones one by one using xargs into docker rm command.

Awesome right? Well, what if you only wanted to remove only the ones that have been STOPPED? Here you go:

As you can see, this command only removes the containers that have ‘Exited’.

To stop all running containers:

Some helper functions:

Add these to your .profile or .bash_profile or whatever shell profile you use. Once added, to apply in existing shell, run:

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.