List only stopped Docker containers

DockerContainers

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

Only stopped containers can be listed using:

docker ps --filter "status=exited"

or

docker ps -f "status=exited"

Solution 2 - Docker

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

Solution 3 - Docker

docker container list -f "status=exited"

or

docker container ls -f "status=exited"

or

 docker ps -f "status=exited"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYogesh_DView Question on Stackoverflow
Solution 1 - DockerYogesh_DView Answer on Stackoverflow
Solution 2 - DockerBMitchView Answer on Stackoverflow
Solution 3 - DockerArtur KarboneView Answer on Stackoverflow