Inspecting Network

Difficulty: Intermediate

Question

How do you find the IP address of a running container, and why should you usually not rely on that IP?

Answer

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.

Where the IP comes from

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.

Useful inspect variations

  • 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.

Why you should not hardcode container IPs

  • IPs are assigned when the container connects to the network; recreate it (docker compose up after a change, a deploy, a crash with --rm) and it can get a different one.
  • On a user-defined network, Docker's embedded DNS server (listening at 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.
  • The default 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).
  • With --network host the container has no IP of its own; it shares the host's stack.

Gotcha on Docker Desktop

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.

How one container finds another. Name-based discovery on a user-defined network means IP changes do not break anything.
  1. api connects to postgres:5432: App config uses the service name.
  2. Embedded DNS at 127.0.0.11: Docker resolves names on user-defined networks.
  3. Returns current container IP: For example 172.18.0.3 on this run.
  4. Traffic over the bridge: veth pair into the network's Linux bridge.
  5. postgres container: Same name keeps working after recreation.

Code examples

Getting a container's IP addresses

# 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.

Use names, not IPs

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.

Key points

  • IPs live under NetworkSettings.Networks, one entry per attached network.
  • Container IPs can change when containers are recreated; use names instead.
  • User-defined networks provide DNS via 127.0.0.11; the default bridge does not resolve names.
  • On Docker Desktop, container IPs are not reachable from the host; use published ports.
  • docker network inspect maps IPs back to container names.

Common follow-up questions

  • Why does container name resolution not work on the default bridge network?
  • How do you assign a static IP to a container?
  • How do containers on two different networks talk to each other?
  • How does a container reach a service running on the host machine?

Interview tips

  • Give the command, then immediately say you would not hardcode the IP; that second half is what interviewers want.
  • Mention the embedded DNS at 127.0.0.11 and the default bridge limitation.
  • Bring up Docker Desktop routing differences if the interviewer mentions macOS or Windows.

Concepts covered

docker inspect, Go templates, Bridge network, Embedded DNS, Service discovery

More Docker & Containers questions