13

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

Ravi Delixan
  • 2,584
  • 1
  • 22
  • 31
  • 1
    You can use the command `docker container list --all` to see all the container no matter the state, and then use the command `docker rm containerName` to delete the container. – Charles Xu Jan 20 '19 at 05:36
  • Possible duplicate of [How to remove old Docker containers](https://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers) – David Maze Jan 20 '19 at 12:22

2 Answers2

36

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.

takacsmark
  • 3,933
  • 23
  • 26
1

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

manga
  • 11
  • 5