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?
The typical command is:
However, this will only list one of the possible non-running statuses. Here's a list of all possible statuses:
You can filter on multiple statuses by passing multiple filters on the status:
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:For more details on filters you can use, see Docker's documentation: https://docs.docker.com/engine/reference/commandline/ps/#filtering
Only stopped containers can be listed using:
or