Docker-py: Status of docker container not updating when container is running

Created on 22 Feb 2021  路  4Comments  路  Source: docker/docker-py

I am running:

container = client.containers.run('alpine', ['tail', '-f' '/dev/null'], detach=True)

then doing

container.status

returns 'created'.

However, running docker ps directly from the shell I see my container is running and also docker container inspect ... correctly shows the status of the container is running.

Most helpful comment

I believe that this SDK is designed to be similar to the rest API and, in that case, get container will do the trick.

Maybe a maintainer can answer you better on this.

:)

All 4 comments

Hi @AdamWRichardson

That happens because the container object return just after the creation, and the container was not running at the time.

You can call the API again, like:

>>> container = client.containers.run("alpine", "tail -f /dev/null", detach=True)
>>> container.status
'created'
>>> updated_container = client.containers.get(container.id)
>>> updated_container.status
'running'

Amazing thank you! I have to say I was a little surprised since the status is implemented as a property that it wasn't fetching the status from the daemon each time it's accessed. Is it possible to do this or has is been designed to maintain state on creation for efficiency reasons?

I believe that this SDK is designed to be similar to the rest API and, in that case, get container will do the trick.

Maybe a maintainer can answer you better on this.

:)

Hi @AdamWRichardson

Could you close this issue?

Thanks and have a nice day :)

Was this page helpful?
0 / 5 - 0 ratings