Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove docker containers that are not running?

Tags:

docker

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


2 Answers

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.

like image 199
takacsmark Avatar answered Sep 13 '25 02:09

takacsmark


Also, to delete a specific container along with its volumes (as @Harlin questioned) you can use the following command: docker rm --volumes container-name. Official doc here: https://docs.docker.com/engine/reference/commandline/rm/#volumes

like image 29
manga Avatar answered Sep 13 '25 02:09

manga