Difficulty: Intermediate
How do you find the IP address of a running container, and why should you usually not rely on that IP?
Run docker inspect and read NetworkSettings.Networks, or use a Go template: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container>. A container gets one IP per network it is attached to, and that IP can change when the container is recreated, so other containers should reach it by name through Docker's built-in DNS on a user-defined network, not by IP.
When a container joins a bridge network, Docker creates a virtual Ethernet pair, plugs one end into the network's Linux bridge (docker0 for the default bridge, br-<id> for user-defined ones) and assigns an address from that network's subnet. The default bridge usually uses 172.17.0.0/16, so the first container often gets 172.17.0.2.
Because a container can be on several networks, NetworkSettings.Networks is a map keyed by network name. The template above loops over it; on a multi-network container it prints the addresses run together, so it is often better to ask for a specific network or print name and IP pairs.
docker inspect <c> --format '{{json .NetworkSettings.Networks}}' for the full JSON, then pipe to jq.docker inspect -f '{{.NetworkSettings.Networks.backend.IPAddress}}' <c> for one named network (use the index function if the name contains a hyphen).docker network inspect <network> lists every container attached to a network with its IP, which is handy when you are tracking down "which container is 172.18.0.5?".docker port <c> shows published ports, which is usually what you actually need from the host.The older top-level NetworkSettings.IPAddress field only describes the default bridge and is empty for containers on user-defined networks, so do not rely on it.
docker compose up after a change, a deploy, a crash with --rm) and it can get a different one.127.0.0.11 inside each container) resolves container names and Compose service names. Your app should connect to postgres:5432, not 172.18.0.3:5432.bridge network does not provide name resolution between containers. This is one of the main reasons to always create your own network (Compose does this for you).--network host the container has no IP of its own; it shares the host's stack.On macOS and Windows, containers run inside a Linux VM, so the container IP is not routable from your laptop. From the host, use published ports (-p 8080:80 then localhost:8080). From a container, reach a service on the host with host.docker.internal.
# All IPs, one per network, as name=ip pairs
docker inspect -f '{{range $name, $net := .NetworkSettings.Networks}}{{$name}}={{$net.IPAddress}} {{end}}' orders-api
# A network whose name contains a hyphen needs index
docker inspect -f '{{(index .NetworkSettings.Networks "shop-net").IPAddress}}' orders-api
# Every container on a network with its address
docker network inspect shop-net -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'
Looping over the Networks map with name and value avoids the merged output you get on multi-network containers.
docker network create shop-net
docker run -d --name postgres --network shop-net -e POSTGRES_PASSWORD=secret postgres:17
docker run --rm --network shop-net busybox nslookup postgres
On a user-defined network the embedded DNS resolves container names, so applications never need to know an IP.
docker inspect, Go templates, Bridge network, Embedded DNS, Service discovery