Inspecting a stopped docker container on python using inspect_container

1.5k Views Asked by At

I'm coding tests with 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:

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

any ideas?

1

There are 1 best solutions below

0
On

You are using a older version of 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