How to remove docker containers that are not running?

It's possible to remove containers that aren't running?

I know that for example, this will remove containers from created images

docker rm `docker ps -q -f status=exited`

But I would like to know how I can remove those that are not running


Solution 1:

Use the docker container prune command, it will remove all stopped containers. You can read more about this command in the official docs here: https://docs.docker.com/engine/reference/commandline/container_prune/.

Similarly Docker has commands for docker network prune, docker image prune and docker volume prune to prune networks, images and volumes.

I use docker system prune most of the time, it cleans unused containers, plus networks and dangling images.

If I want to clean volumes with the system components, then I use docker system prune --volumes. In this case unused volumes will be removed, too, so be careful, you may loose data.