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.

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.

List of running containers,

Get Docker container IP address from the Host

Get a Docker container IP address from host using Container Name

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

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,

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

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,

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

It will return a list of all the running containers i.e.

Now let’s pass this list of container IDs to docker inspect command and fetch name of containers only i.e.

It will return the name of all the running containers i.e.

Now we can also get the Name and IP of all running containers too using inspect command i.e.

It will return the name and IP address of all the running containers i.e.

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,

For example,

It will display network settings like this,

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.

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,

Output:

Similarly, to fetch the complete configuration details use,

Output:

Complete output of docker inspect command is,

Output :

 

 

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.