Categories
docker ip-address

How to get a Docker container’s IP address from the host

1925

Is there a command I can run to get the container’s IP address right from the host after a new container is created?

Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts.

1

  • 50

    I just wanted to make sure other noobs don’t make my mistake and try to get the IP from the image instead of the container. Ensure you get the CID or container id and query that; CID via ‘docker ps’ that is.

    Jan 19, 2015 at 18:06

3216

+350

The --format option of inspect comes to the rescue.

Modern Docker client syntax is:

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

Old Docker client syntax is:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

These commands will return the Docker container’s IP address.

As mentioned in the comments: if you are on Windows, use double quotes " instead of single quotes ' around the curly braces.

24

  • 47

    This is a much, MUCH better solution. Only ask for what you need, if possible!

    – MikeyB

    Jan 7, 2014 at 2:19

  • 14

    Hmm. Getting a <no value> response on a Mac using Fig/Boot2Docker

    – cevaris

    Nov 24, 2014 at 14:57

  • 78

    !WARNING! This doesn’t work anymore. The new format is specific to the container and follows the form {{ .NetworkSettings.Networks.$network.IPAddress }}. The default appears to be bridge, but under docker-compose this will be a specific name that depends on the name of your app (I think from the –project-name flag, though that’s also going to depend on what type of networking config you have set up). I wrote a full answer in an answer here stackoverflow.com/questions/17157721/…

    – Dunk

    Feb 10, 2016 at 15:46


  • 9

    Maybe I am a sleepy sheep, but the warning about double quotes was not bold and blinky enough to catch my attention. Maybe provide two examples clearly labelled Linux and Windows. I literally just typed the example, checked the docs which also uses single quotes and then Googled the error. I’m sure I’m not alone.

    – Wyck

    Apr 15, 2019 at 18:42

  • 3

    Solution for peoples getting formatting error: docker inspect <container id> and scroll down you will find the container ip-address

    Jun 28, 2019 at 17:13


566

You can use docker inspect <container id>.

For example:

CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID

7

  • 69

    In order to extract the ip, you can do something like docker inspect $CID | grep IPAddress | cut -d '"' -f 4, it works fine 🙂

    – creack

    Jun 17, 2013 at 23:39

  • 12

    Bringing it all together, this shell alias should list all container ids and their ips: alias dockerip='docker ps | tail -n +2 | while read cid b; do echo -n "$cid\t"; docker inspect $cid | grep IPAddress | cut -d \" -f 4; done'

    – ko-dos

    Dec 12, 2013 at 11:22


  • 66

    As mentionned by @user3119830, there is a new option to inspect. Now, you can get the Ip easier with docker inspect -format '{{ .NetworkSettings.IPAddress }}' ${CID}

    – creack

    Jan 7, 2014 at 2:48

  • 17

    Just a note. The single dash options are being deprecated so that -format will become –format.

    – jamtur01

    Mar 22, 2014 at 1:42

  • 8

    docker inspect -format '{{ .NetworkSettings.IPAddress }}' ${CID} is the new syntax. -format is deprecated, it becomes --format.

    – Devy

    Aug 10, 2015 at 22:09

535

First get the container ID:

docker ps

(First column is for container ID)

Use the container ID to run:

docker inspect <container ID>

At the bottom, under NetworkSettings, you can find IPAddress

Or just do for UNIX based:

docker inspect <container id> | grep "IPAddress"

And for Windows CMD:

docker inspect <container id> | findstr "IPAddress"

9

  • 10

    Too bad, on one of my instances (started with docker run -it MYCONTAINER /bin/bash), the output of inspect has no section NetworkSettings!

    – Eric

    Oct 12, 2017 at 10:16


  • 5

    This doesn´t work on Windows 10. You need too find out the ip address of DockerNAT with ipconfig.

    – ntiedt

    Nov 7, 2017 at 15:30

  • 1

    @Eric You can also inspect network bridge for that: docker network inspect bridge | grep Gateway | grep -o -E '[0-9.]+'

    Feb 13, 2018 at 7:12

  • 6

    I greatly prefer the ... | grep IP solution to all the formatting details required to get IP. +1

    – d8aninja

    Mar 6, 2019 at 17:49

  • 3

    @Eric – you’ve confused container with image. The command is actually docker run -it MYIMAGE (that isn’t a container name); if you do docker inspect MYIMAGE you will get static information about the image. You need to find the name of the running container (or use its id as others suggest). On my system, the first such container created from that image defaults to name MYIMAGE_1, so docker inspect MYIMAGE_1 has NetworkSettings.

    Mar 29, 2019 at 19:27