brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: one docker inspect template prints the IP of a container on each network

Docker
~/tips/tip-docker-inspect-container-ip

docker inspect --format '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}={{$v.IPAddress}} {{end}}' myapp

Get a container's IP addresses per network without scrolling through JSON.

A container on two networks has two addresses, and they are buried deep in docker inspect. A short Go template lists them with the network names.

docker inspect --format '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}={{$v.IPAddress}} {{end}}' myapp

The output looks like bridge=192.0.2.4 proxy=198.51.100.3 (example addresses). The range loops over the networks map, $k is the network name and $v.IPAddress the address on it.

For just the addresses, the shorter form works too:

docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' myapp

I think you mostly need this to test a service from the host with curl, or when a firewall rule needs the container’s address.

Do not hard code what you find. Container addresses on Docker networks are handed out at start, and a recreated container usually gets a different one. Between containers, use the service or container name on a shared user defined network instead. A stopped container has no address at all. And on Docker Desktop these addresses live inside the VM, so your Mac or Windows host cannot reach them directly.

Further reading: Format command and log output, docker container inspect

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link