How o get the IP address of docker container using docker SDK for python?


I am launching a container with network_mode = bridge, when I inspect the network container in the terminal I get the container IP address.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id

However, I can’t get this IP address from the container object.
Is there any solution without searching the container in the network object?

Asked By: Tiago Ramalho



Here is an example:

import docker

client = docker.DockerClient()
container = client.containers.get("magical_meitner")
ip_add = container.attrs['NetworkSettings']['IPAddress']
Answered By: LinPy

If the IP doesn’t return from container.attrs['NetworkSettings']['IPAddress'] as suggested here, try the following:

network_name = "my_net"

For me, this was the case when the containers were created with docker-compose, with an external network.

Answered By: Gulzar
container.attrs.get("NetworkSettings", {}).get("Networks", {}).get(, {}).get("IPAddress")
Answered By: edsion zhang
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.