Sometimes you need a container’s IP address — for debugging, a firewall rule, or a quick curl. Docker makes it easy to find. But before you hard-code one anywhere, read the second half of this article.

Get the IP of one container

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

A container can be attached to several networks, with one IP on each. Show them with network names:

docker inspect -f '{{range $name, $net := .NetworkSettings.Networks}}{{$name}}: {{$net.IPAddress}}{{"\n"}}{{end}}' mycontainer

With jq:

docker inspect mycontainer | jq -r '.[0].NetworkSettings.Networks | to_entries[] | "\(.key): \(.value.IPAddress)"'

IPs of all running containers

docker ps -q | xargs docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}'

Or per network:

docker network inspect bridge -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'
docker network inspect myproject_default

From inside the container

docker exec mycontainer hostname -i
docker exec mycontainer ip addr       # if iproute2 is installed in the image
cat /etc/hosts                        # inside: the container's own IP is listed

Containers with no IP shown

  • --network host — the container uses the host’s network stack directly; it has no separate IP. Use the host’s address.
  • --network none — no networking.
  • Stopped containers — IPs are assigned at start; a stopped container has none.

Why you shouldn’t hard-code container IPs

Container IPs are dynamic. When a container is recreated — on every image update, docker compose up after a config change, or a reboot — it may get a different address. Anything pointing at the old IP breaks.

Use names instead. On user-defined networks (and every Docker Compose project), Docker runs an internal DNS server: containers reach each other by container name or Compose service name.

services:
  app:
    image: ghcr.io/example/app:1.4
    environment:
      DATABASE_HOST: db         # the service name, not an IP
  db:
    image: postgres:17

The app connects to db:5432, whatever IP the database currently has. See Docker networking.

From the host, don’t connect to container IPs either — publish the port on localhost and use 127.0.0.1:

    ports:
      - "127.0.0.1:5432:5432"

A reverse proxy on the host then forwards to 127.0.0.1:PORT — stable across container recreation.

If you really need a fixed IP

Some setups (legacy software, firewall rules, specific network appliances) genuinely need a static address. Define a network with a subnet and assign one:

networks:
  appnet:
    ipam:
      config:
        - subnet: 172.30.0.0/24

services:
  legacy:
    image: example/legacy
    networks:
      appnet:
        ipv4_address: 172.30.0.10

Static IPs only work on user-defined networks with a configured subnet. Keep them to the minimum — every hard-coded address is something to maintain.

Getting the host’s IP from a container

The reverse question — a container that needs to reach a service on the host:

docker run --add-host=host.docker.internal:host-gateway myimage

The container can now use host.docker.internal. The host service must listen on an address reachable from the Docker bridge (not only 127.0.0.1).

Quick reference

Task Command
One container’s IP docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' NAME
All containers docker ps -q | xargs docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
Containers on a network docker network inspect NET
Connect between containers Use the service/container name

More commands: Docker cheat sheet.

  • Docker
  • Networking
  • Containers