Remove all Docker containers/images

by|inArticles||2 min read
Docker
Docker

Docker makes it simple to create containers for your apps, which means that they can be run on any machine. However, as you get stuck in the weeds with Docker and start to needlessly accumulate unused images, containers and data volumes its important to remove them often.

Docker provides a whole set of commands for your system, and can help you clean up from all angles. This article provides a quick reference to these commands, enabling you to free up disk space, keep things organized by removing unneeded docs - and more, with just one command.

Stop all Docker Containers

If you would like to stop all docker containers, you can just use docker ps to list all containers and combine this list to stop each container that is in the list. Before we can remove all containers we need to stop all docker containers first. Otherwise docker will refuse to remove containers that are still running (hence, in use).

docker stop $(docker ps -a -q)

As soon as you have stopped all containers with the given command you can proceed with removing images. Containers are - generally speaking - instances of images. As soon as all containers are stopped, we can as well remove images. We will see in the next section on how to achieve this.

Remove All Docker Containers

To remove all containers we just need to combine the command to list all containers (docker ps -a -q) with the rm command. The rm command stands for remove and targets containers. By combining both commands in one line we can remove all docker containers in one step:

docker rm $(docker ps -a -q)

Remove All Docker Images

If you would like to remove all images just use the rmi command. But instead of ps (to list all containers) we use the images command to get a list of all images. With this small adjustment docker can find and remove all images in one step:

docker rmi $(docker images -q)

That's it. Simple!

Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

Related Articles

© Copyright 2024 - ersocon.net - All rights reservedVer. 415