Along with removing Docker containers, you’ll also learn how to remove Docker images, volumes, and networks. This is useful when you want to completely purge all traces of containers from your Docker installation and start fresh. Check out the examples below to find out how.
In this tutorial you will learn:
- How to list, stop, and remove all Docker containers
- How to remove Docker images, volumes, and networks
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Removing Docker containers command line examples
- Start by using the following command to see a list of all your Docker containers. This will also list the container ID, in case you want to remove containers individually. Review this list carefully to make sure you actually want to remove all of these containers.
$ docker container ls -a
- You can now use the
docker container rmcommand to remove the containers you don’t want. You just need to specify the container ID of each one that you want to remove. Here’s an example where we move a couple from the list above:
$ docker container rm 6700495a31b6 20e038f97de1
Note that this command will only work for containers that aren’t running. Otherwise, you will encounter an error like the one below.
Error response from daemon: You cannot remove a running container.
- To remove all stopped containers at once, you can use the following command.
$ docker container prune
- To stop all containers so that they can all be removed, you can use the following command.
$ docker stop $(docker ps -aq)
- After running the previous command, you can use this one to remove all Docker containers on your system.
$ docker container rm $(docker container ls -aq)
- You can remove all Docker images from your system with the following command.
$ docker rmi $(docker images -q)
- To remove all unused Docker volumes, execute the following command.
$ docker volume prune
- Lastly, to remove all unused Docker networks, use this command.
$ docker network prune
In this guide, we saw various examples for removing all Docker containers from a Linux system. This allows you to start fresh very quickly, without fiddling around with removing containers individually. We also learned how to remove Docker images, volumes, and networks, allowing us to clean up our Docker installation even more thoroughly.