Docker-py: refresh container.attrs

Created on 3 Jan 2017  路  6Comments  路  Source: docker/docker-py

Following code runs into a problem:

container = docker.from_env().containers.create(image=...)
container.start()
ip = container.attrs["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]
# ip == "" :(, but I get it.
time.sleep(...)
ip = container.attrs["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]
# ip == "" :(, still ???:-(???
groudocumentation levedockerclient

Most helpful comment

I think this is the same problem that I was about to open an issue for -- the documentation for running a container does not mention that Container.attrs is cached (specifically in the detach=True example). The Container.reload method is not mentioned in the documentation (nor is wait by the way), is this an oversight or should it not be called?

my code for running a command in two docker images ended up looking like this:

def run_comparison_cmd(old_image, new_image, command, block=True):
    print("Running command '{}' in containers {}, {}".format(command, old_image, new_image))
    client = docker.from_env()
    old = client.containers.run(old_image, command, detach=True)
    new = client.containers.run(new_image, command, detach=True)

    if block:
        print("Containers started, waiting for them to finish")
        old.wait()
        new.wait()

        #attributes are cached, update them
        old.reload()
        new.reload()

    return old, new

should the wait method on Container be updated to perform a reload before returning?

All 6 comments

I have run into a similar problem with myself -- try running container.reload()

I think this is the same problem that I was about to open an issue for -- the documentation for running a container does not mention that Container.attrs is cached (specifically in the detach=True example). The Container.reload method is not mentioned in the documentation (nor is wait by the way), is this an oversight or should it not be called?

my code for running a command in two docker images ended up looking like this:

def run_comparison_cmd(old_image, new_image, command, block=True):
    print("Running command '{}' in containers {}, {}".format(command, old_image, new_image))
    client = docker.from_env()
    old = client.containers.run(old_image, command, detach=True)
    new = client.containers.run(new_image, command, detach=True)

    if block:
        print("Containers started, waiting for them to finish")
        old.wait()
        new.wait()

        #attributes are cached, update them
        old.reload()
        new.reload()

    return old, new

should the wait method on Container be updated to perform a reload before returning?

I think this is a serious Issue as Container.create returns a container-object. And I'm especially interested in its ExitCode (attrs['State]['ExitCode'], But this always remains 0, even if the container is exited with a non-zero ExitCode.
Even its status remains unchanged ('created') instead of ('exited').

I think, that the container-object you get back from containers.create() is actually nothing else than the response of the docker-api and does not follow the changing state of it's container. With the package, its like calling an api and you have to call client().containers.get(container.id) again, if you want to have updated information

However, this should definitely be mentioned in the Documentation more clearly - as the description of a container-object is completely misleading!

I meet this problem too. With the help of @ah19 , I solve it.

At the same time, I agree with @lgilz.

man, this was painful. thanks all.

actually, reload doesn't get the 'HostConfig' up-to-date. i still do not see my exposed ports

Was this page helpful?
0 / 5 - 0 ratings