285

Docker gives you a way of listing running containers or all containers including stopped ones.

This can be done by:

$ docker ps # To list running containers

Or by

$ docker ps -a # To list running and stopped containers

Do we have a way of only listing containers that have been stopped?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yogesh_D
  • 17,656
  • 10
  • 41
  • 55

3 Answers3

416

Only stopped containers can be listed using:

docker ps --filter "status=exited"

or

docker ps -f "status=exited"
Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
  • 6
    Also, you can filter them with `grep` for example `docker ps -a | grep Exited` or something like that. – starikovs May 14 '15 at 08:08
  • 28
    Cool, now I can remove stopped containers with `docker rm $(docker ps --filter "status=exited" -q)` – czerasz Jan 19 '16 at 11:18
  • 2
    better yet create an alias in your bash profile and use a shorter keyword/command to clean up stopped containers – Yogesh_D Jan 20 '16 at 08:23
  • 28
    "docker container prune" can be used since 1.25 I believe to remove stopped containers – paul Sep 29 '17 at 10:57
68

The typical command is:

docker container ls -f 'status=exited'

However, this will only list one of the possible non-running statuses. Here's a list of all possible statuses:

  • created
  • restarting
  • running
  • removing
  • paused
  • exited
  • dead

You can filter on multiple statuses by passing multiple filters on the status:

docker container ls -f 'status=exited' -f 'status=dead' -f 'status=created'

If you are integrating this with an automatic cleanup script, you can chain one command to another with some bash syntax, output just the container id's with -q, and you can also limit to just the containers that exited successfully with an exit code filter:

docker container rm $(docker container ls -q -f 'status=exited' -f 'exited=0')

For more details on filters you can use, see Docker's documentation: https://docs.docker.com/engine/reference/commandline/ps/#filtering

BMitch
  • 231,797
  • 42
  • 475
  • 450
27
docker container list -f "status=exited"

or

docker container ls -f "status=exited"

or

 docker ps -f "status=exited"
Artur Karbone
  • 1,456
  • 13
  • 11