How to Use Docker Inspect

First of all, what is docker inspect? Docker inspect is a tool that enables you do get detailed information about your docker resources, such as containers, images, volumes, networks, tasks and services. In this article, I will go through examples of how you can use docker inspect to get detailed information about your docker resources, which will aid you in the management and troubleshooting of your docker objects.

The basic syntax for using docker inspect is:

$ docker inspect [OPTIONS] NAME

By default, docker inspect will render it’s output in a JSON array. First of all we will have a look at how to inspect docker images.

Get detailed Docker Image information using Docker Inspect

You can get detailed information on a docker image using docker inspect. For example:

$ docker inspect nginx

This will output information on the nginx image. The output is quite long due to the amount of information. A section of it looks like this:

         "Architecture": "amd64",
"Os": "linux",
"Size": 132089228,
"VirtualSize": 132089228,

Filtering Docker Inspect Image Data

Due to the volume of information returned when using the docker inspect command, it’s often necessary to apply a filter to the output. We can do so by using the –format flag when running the command. For example, if we just wanted to list the OS type used in the image we could do so by using:

$ docker inspect --format='{{.Os}}' nginx
linux

This command has returned the value ‘linux’ as expected. We can also return the value of a nested property. For example:

"Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },

The exposed ports property is nested under the config property. We can return the value easily by running:

$ docker inspect --format='{{.Config.ExposedPorts}}' nginx

The syntax of the format options used here is Go. You can read a lot more about Go templates here, including how to do some more complex formatting!

How to Inspect a Docker Container

We can use docker inspect to get details on a docker container. The basic syntax is the same as when using the command on a docker image, but instead you use the container name. For example:

docker inspect affectionate_jennings

This will generate quite a comprehensive output. Some of the information available here includes:

  • Container state – e.g whether the container is running, when it started, PID
  • Image
  • Log path
  • Storage driver
  • DNS config
  • Memory and CPU config
  • Exposed Ports
  • Environment variables
  • Volumes
  • Network config

Lets have a look at some examples.

How to list a Docker Containers IP address and other network settings

We can list a containers IP address easily by applying a filter to the docker inspect container output:

$ docker inspect --format='{{.NetworkSettings.IPAddress}}'  affectionate_jennings
172.17.0.2

We can list all the containers network settings using:

$ docker inspect --format='{{json .NetworkSettings}}'  affectionate_jennings

List A Docker Containers Hostname

To list the hostname of a docker container we can use the following:

$ docker inspect --format='{{ .Config.Hostname }}'  affectionate_jennings

We could also combine this with the IP address by using:

$ docker inspect --format='{{.Config.Hostname}} has the following IP: {{.NetworkSettings.IPAddress}}'  affectionate_jennings

This will give the following output:

4ffd6d1598cc has the following IP: 172.17.0.2

How to List the Size of a Container

You can list the size of a container easily using docker inspect, by using the -s flag. For example:

$ docker inspect -s d2cc496561d6
              [
              {
              "SizeRw": 0,
              "SizeRootFs": 972,
              }
              ]

How to Inspect Docker Volumes

Docker inspect can also be used to list information about docker volumes. The data returned is shorter than that of images or containers. Here’s an example of the complete output when running the command:

$ docker inspect 7d3c889157b564c14208334ae490f4ef161dac0b4e44e4b4daff346b04bd64c7
[
    {
        "CreatedAt": "2020-06-29T13:27:47Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": /var/lib/docker/volumes/7d3c889157b564c14208334ae490f4ef161dac0b4e44e4b4daff346b04bd64c7/_data",
        "Name": 7d3c889157b564c14208334ae490f4ef161dac0b4e44e4b4daff346b04bd64c7",
        "Options": {},
        "Scope": "local"
    }
]

As you can see, the output lists the volume name, mountpoint, driver, and creation date/time.

Output Docker Inspect as JSON

We can keep the output as JSON, whist still filtering the output by using:

$ docker inspect --format='{{json .Config}}' nginx

How to Use jq to format Docker Inspect JSON output

JSON can sometimes be tricky to read if its a long document. jq is a linux command line tool for formatting and manipulating JSON strings. It can be installed on Centos by running:

$ yum install jq

It is a powerful tool, but one simple thing we can do with it is to have it colourize our docker inspect output. To do so, we simply need send the output to the jq application:

$ docker inspect --format='{{json .NetworkSettings.Networks }}' friendly_hoover | jq .

Rather than the standard output, we will instead get a colorized output which is a little easier on the eyes!

Useful Tip – Be Specific when Inspecting Docker Resources

As you now know, the syntax when using docker inspect is:

 $ docker inspect [OPTIONS] NAME 

It’s the same each time – docker inspect, then the name of the resource you wish to inspect. But what do you do if you have two resources with the same name? For example, you may have a image called ‘nginx’ and you may also have a container called ‘nginx’ on the same docker host.

The solution for this problem is to use the –type flag when running the command. The type flag allows us to specify what type of resource we are wanting to inspect. For example, we could inspect the image using:

docker inspect --type=image nginx

To inspect a container you can use:

docker inspect --type=container nginx

Final Thoughts…

Hopefully this has been useful and you now have an idea of what is docker inspect how you can use docker inspect to list details about docker containers, images and other resources!

Related posts

Docker Exec Command With Practical Examples

Debugging with Git Bisect

A Beginners Guide to Azure Repos

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More