Because of business needs, we need to use Python to clean up some container that are dead, of course, we can use Docker ps-a and then filter the container with the status of dead, now we have a better choice: docker-py
Official Document Address: https://docker-py.readthedocs.io/en/stable/
Here's a simple use:
>>> from Docker import Client
>>> CLI = Client (base_url= ' Unix://var/run/docker.sock ')
And then we're going to get all the exited and dead containers.
>>> exited_containers = Cli.containers (quiet=true, filters={' status ': ' exited '}) #get all the exited container
>>> dead_containers = Cli.containers (All=true, quiet=true, filters={' status ': ' Dead '}) #get all the exited Container
Then we can see Exited_containers and dead_containers, and then we can get all the container_id through the loop
For container in Exited_containers:
print ' Container ID is: ', container[' id ']
And then we can call delete the
Cli.remove_container (container=container[' Id ')