简体   繁体   English

使用inspect_container在python上检查停止的docker容器

[英]Inspecting a stopped docker container on python using inspect_container

I'm coding tests with python. 我正在用python编写测试代码。 I want to make a method that outputs the status of a container (running/exited). 我想制作一个输出容器状态(运行/退出)的方法。

import docker

class Container:
    def __init__(self, name, image, *, command=[], links={}):
        self._docker = docker.DockerClient(base_url='unix://var/run/docker.sock')

    def get_status(self):
         inspection = self._docker.api.inspect_container(self.id)
         return inspection['State']['Status']

this method (get_status) works when container is running but fails when container is stopped, with this error message: 此方法(get_status)在容器运行时有效,但在容器停止时失败,并显示以下错误消息:

E       docker.errors.NotFound: 404 Client Error: Not Found ("No such container: 2457e5a283e5cb4add4fdb36pb465437b21bb21f768be405fe40615e25442d6e

"docker inspect" cli command works on the instance when it is stopped, but I need to do it through python 当实例停止时,“ docker inspect” cli命令可在该实例上运行,但我需要通过python来完成

any ideas? 有任何想法吗?

You are using a older version of docker-py . 您正在使用旧版本的docker-py Do below 在下面做

pip uninstall docker-py
pip install docker

then run the code 然后运行代码

import docker
client = docker.client.DockerClient()
container = client.containers.get("2457e5a283e5cb4add4fdb36pb465437b21bb21f768be405fe40615e25442d6e")

Now it should work 现在应该可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM