snippets > get-docker-container-ip-command-line

November 14, 2020

Get a Docker container's IP from the command line

When dealing with container IPs, it’s important to take note of which network each container is conected to and which network mode it’s using. The default network mode for a Docker installation is bridge, so I’ll assume you’re using it too.

Any container started with docker run will be attached to a default bridge network. To get its IP on that network, run:

docker inspect --format "{{.NetworkSettings.Networks.bridge.IPAddress}}" <container_name>

Note: the --format flag accepts Go templating syntax. Take a look at some Docker usage examples.

For containers started with docker-compose, a new bridge network is created. Its default name will be <folfer_name>_default, so the above command would look like this:

docker inspect --format "{{.NetworkSettings.Networks.<folfer_name>_default.IPAddress}}" <container_name>

In both cases, the received IPs are reachable from the host and from other containers in the same bridge.

Further reading

Official docs on Docker bridge networks