docker get container IP address with docker inspect

# Get a container's IP from the host
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-container

# Get a container IP on a named network
docker inspect -f '{{(index .NetworkSettings.Networks "my-net").IPAddress}}' my-container

# List IPs for all running containers
docker inspect $(docker ps -q) --format '{{.Name}}: {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

You need to know the Docker container IP address for debugging, direct connection, or checking which container is attached to a network.

Get container IP from the host by name

Use the container name or ID with docker inspect. This is the most common answer for searches like "docker get container ip" and "docker container ip address command".

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

# If you only remember part of the name:
docker ps --format '{{.Names}}'
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-container

IP on a specific named network

A container can be attached to more than one network. Query the network name when you need the IP on a specific Docker network.

# Replace "my-net" with your network name
docker inspect -f '{{(index .NetworkSettings.Networks "my-net").IPAddress}}' my-container

List all IPs for all running containers

Use this when you want a quick inventory of container names and IP addresses from the host.

docker inspect $(docker ps -q) --format '{{.Name}}: {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

Get IP from inside the container

If you are already inside the container, hostname -i or ip addr can show the container-side address.

docker exec my-container hostname -i
# or
docker exec my-container ip addr show eth0

Prefer container names over IP addresses

On user-defined Docker networks, containers resolve each other by name. Names are more stable than IP addresses, which can change when containers restart.

# Use the service/container name in connection strings
DATABASE_URL=postgres://db:5432/mydb   # "db" is the container name