Introduction to methods for managing Docker using Docker-py

Source: Internet
Author: User
Tags docker ps

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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.