In this article we will discuss how to get ip address assigned to a running docker container from host using docker inspect command.

docker inspect

Docker command line interface  provides a command docker inspect to get the low level information about the docker container i.e.

docker inspect <Option> <container name | ID >

It returns a lot of low level information about the running docker container in JSON format. In that information we get the assigned IP address of the container too. 

But, apart from IP address it returns too much information. If we want some specific information then we can use the –format (-f) flag. It can format the JSON output of docker inspect using the given Go template.

Let’s see some examples,

Suppose we have a running docker container i.e.

docker ps

List of running containers,
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1caec9cde092        centos              "/bin/bash"         3 seconds ago       Up 2 seconds                            hungry_williamson

Get Docker container IP address from the Host

Get a Docker container IP address from host using Container Name

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

 It will return the IP address of container with name hungry_williamson i.e.
172.17.0.2

Using a GO template we filtered the output of docker inspect command to fetch IP address only.

Get a Docker container IP address from host using container ID

Similarly we can fetch the IP address using container ID instead of name in previous command. Like this,

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 1caec9cde092

It will return the IP address of container with name 1caec9cde092 i.e.
172.17.0.2

So, this way we got the IP address of a docker container from host. But what if we have 100 running containers and we want their IP addresses along with name ?

Get IP Address and names of ALL the running containers from host

Suppose we have 3 running containers,

PS C:\Windows\system32> docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
915d14f022e9        ubuntu              "/bin/bash"         44 seconds ago      Up 43 seconds                           boring_dubinsky
21ea36eb1663        centos              "/bin/bash"         52 seconds ago      Up 51 seconds                           tender_raman
a257a6e955b8        centos              "ping localhost"    4 hours ago         Up 4 hours                              quizzical_banzai

Now we want to fetch the name and IP address of all the running containers. Let’s see how to do that,

To get the IDs of all running containers use following command

$(docker ps -q)

It will return a list of all the running containers i.e.
915d14f022e9
21ea36eb1663
a257a6e955b8

Now let’s pass this list of container IDs to docker inspect command and fetch name of containers only i.e.
docker inspect --format='{{.Name}}' $(docker ps -q)

It will return the name of all the running containers i.e.
/boring_dubinsky
/tender_raman
/quizzical_banza

Now we can also get the Name and IP of all running containers too using inspect command i.e.
docker inspect --format='{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)

It will return the name and IP address of all the running containers i.e.
/boring_dubinsky 172.17.0.4
/tender_raman 172.17.0.3
/quizzical_banzai 172.17.0.2

docker inspect provides many information regarding a container. Here we fetched only couple of fields using GO template.

Main information provided by docker inspect includes, configurations, state, NetworkSettings and many other sections.

Get Complete Network Settings of a docker container

To get the complete network settings of a running docker container using inspect command use this,

docker inspect --format='{{json .NetworkSettings}}'  <container ID>

For example,
docker inspect --format='{{json .NetworkSettings}}'  915d14f022e9

It will display network settings like this,
{
  "Bridge": "",
  "SandboxID": "ca421761e775d17bfea4afd761041d55015be32fece11b1ae468cae581e8fef8",
  "HairpinMode": false,
  "LinkLocalIPv6Address": "",
  "LinkLocalIPv6PrefixLen": 0,
  "Ports": {},
  "SandboxKey": "/var/run/docker/netns/ca421761e775",
  "SecondaryIPAddresses": null,
  "SecondaryIPv6Addresses": null,
  "EndpointID": "0d13ef40107cc275913658b1fe8a2b355dae02eea8e6b38f0426e33afad6d505",
  "Gateway": "172.17.0.1",
  "GlobalIPv6Address": "",
  "GlobalIPv6PrefixLen": 0,
  "IPAddress": "172.17.0.4",
  "IPPrefixLen": 16,
  "IPv6Gateway": "",
  "MacAddress": "01:12:11:11:0a:04",
  "Networks": {
    "bridge": {
      "IPAMConfig": null,
      "Links": null,
      "Aliases": null,
      "NetworkID": "c86b149b1b8c0c26ddd22dc55c4bcdea7f3d9067c89fba1cec0d2072de2500d6",
      "EndpointID": "0d13ef40107cc275913658b1fe8a2b355dae02eea8e6b38f0426e33afad6d505",
      "Gateway": "172.17.0.1",
      "IPAddress": "172.17.0.4",
      "IPPrefixLen": 16,
      "IPv6Gateway": "",
      "GlobalIPv6Address": "",
      "GlobalIPv6PrefixLen": 0,
      "MacAddress": "01:12:11:11:0a:04",
      "DriverOpts": null
    }
  }
}

Now you can use this command to fetch information of other sections too . These section can be,

  • Config
  • GraphDriver
  • Mounts
  • HostConfig
  • Name
  • LogPath
  • State
  • Created
  • etc.

Replace the <SectionName> in below command to get the detailed information about that section.

docker inspect --format='{{json .<SectionName>}}'  <container ID>

You can also directly fetch some specific information inside the section. For example, let’s fetch Image name inside the Config section,

Get Image name from the container ID,

docker inspect --format='{{json .Config.Image}}'  915d14f022e9

Output:
"ubuntu"

Similarly, to fetch the complete configuration details use,
docker inspect --format='{{json .Config}}'  915d14f022e9

Output:
{
  "Hostname": "ccc141f0aabb",
  "Domainname": "",
  "User": "",
  "AttachStdin": true,
  "AttachStdout": true,
  "AttachStderr": true,
  "Tty": true,
  "OpenStdin": true,
  "StdinOnce": true,
  "Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  ],
  "Cmd": [
    "/bin/bash"
  ],
  "ArgsEscaped": true,
  "Image": "ubuntu",
  "Volumes": null,
  "WorkingDir": "",
  "Entrypoint": null,
  "OnBuild": null,
  "Labels": {}
}

Complete output of docker inspect command is,
docker inspect 915d14f022e9

Output :

 

 

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.