List only stopped Docker containers

前端 未结 3 1066
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 21:10

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 contain         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-29 21:17

    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

提交回复
热议问题