docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

I can not get my container's IP address.

Open b1tkeeper opened this issue 5 years ago • 5 comments

I started a container with python sdk, but the sdk cannot obtain the network information of the container (other information is correct), but the network information can be viewed correctly using docker inspect. Help!

b1tkeeper avatar Oct 15 '20 08:10 b1tkeeper

It'll be in container.attrs['NetworkSettings']['IPAddress']

SelfhostedPro avatar Oct 21 '20 19:10 SelfhostedPro

Yeah I've a problem with this also. I take a look a the output for docker inspect <container name> and I can see the "IPAddress" key with an IP address-like value When I take the container.attrs['NetworkSettings']['IPAddress'] as suggested, it is an empty string.

I'm using PyCharm and I've searched the container object in the debug window for what I know the IP address should be (from docker inspect output) but there is no result for the search

eoinmarron avatar Oct 07 '21 12:10 eoinmarron

@eoinmarron

I take a look a the output for docker inspect and I can see the "IPAddress" key with an IP address-like value When I take the container.attrs['NetworkSettings']['IPAddress'] as suggested, it is an empty string.

Exactly the same for me. docker inspect will show me the IP address while the container.attrs won't. I have double checked, the container ID in both docker inspect and container.attrs is exactly the same.

georgms avatar Aug 01 '22 07:08 georgms

So container.attrs appears to be just a snapshot and does not update as the container state changes. In my case, container.attrs was kept at status created without an IP. Refreshing it with something like container = client.containers.get(container.attrs['Id']) would actually reveal a running state, and an IP address.

georgms avatar Aug 01 '22 07:08 georgms

You need to call container.reload() for the container object to reflect the current state of the docker container. This also holds true for the other kinds of docker objects like volumes or networks.

orbelico avatar Aug 17 '22 20:08 orbelico