因为业务需要,需要使用python来清理一些状态为Dead的Container,当然,我们可以使用docker ps -a 然后过滤状态为dead的容器,现在我们有了一个更好的选择:docker-py
官方文档地址:https://docker-py.readthedocs.io/en/stable/
下面说一下简单的使用:
1 2 |
>>> from docker import Client >>> cli = Client(base_url='unix://var/run/docker.sock') |
然后我们要获取所有的exited和dead的containers
1 2 |
>>> 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 |
然后我们可以看到exited_containers 和 dead_containers,然后我们可以通过循环得到所有的container_id
1 2 |
for container in exited_containers: print 'the container id is :', container['Id'] |
然后我们可以调用删除了
1 |
cli.remove_container(container=container['Id']) |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021