How to use docker image inspect command to view image details

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker image inspect command to view detailed information about a Docker image. You will begin by pulling a sample image. Then, you will explore how to inspect the image using the default output format, the JSON format, and a custom Go template to extract specific details. This hands-on experience will equip you with the skills to effectively examine the characteristics and configuration of your Docker images.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/inspect -.-> lab-555155{{"How to use docker image inspect command to view image details"}} docker/pull -.-> lab-555155{{"How to use docker image inspect command to view image details"}} end

Pull a sample image

In this step, you will learn how to pull a Docker image from a registry. Docker images are the building blocks of containers. They are read-only templates that contain the application code, libraries, dependencies, and configuration files needed to run an application.

To pull an image, you use the docker pull command followed by the image name. If you don't specify a tag, Docker will pull the latest tag by default.

Let's pull the hello-world image, which is a very small image used to test if Docker is installed correctly.

docker pull hello-world

You should see output similar to this, indicating that the image is being downloaded:

Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

This output shows that Docker is pulling the latest tag of the hello-world image from the Docker Hub registry (which is the default registry). The lines with Pull complete indicate that the different layers of the image have been downloaded.

Inspect the image with default format

In this step, you will learn how to inspect a Docker image to view detailed information about it. The docker inspect command provides a wealth of information about Docker objects, including images, containers, networks, and volumes. By default, it outputs the information in a structured format.

To inspect an image, you use the docker inspect command followed by the image name or ID. Let's inspect the hello-world image that you pulled in the previous step.

docker inspect hello-world

This command will output a large amount of information about the hello-world image in a default format. The output includes details such as the image ID, creation date, architecture, operating system, and configuration.

[
  {
    "Id": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
    "RepoTags": ["hello-world:latest"],
    "RepoDigests": [
      "hello-world@sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591"
    ],
    "Parent": "",
    "Comment": "",
    "Created": "2023-03-07T21:50:09.004511104Z",
    "Container": "a928b81f51a91111111111111111111111111111111111111111111111111111",
    "ContainerConfig": {
      "Hostname": "a928b81f51a9",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": {}
    },
    "DockerVersion": "20.10.21",
    "Author": "",
    "Config": {
      "Hostname": "",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": null
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 1330,
    "VirtualSize": 1330,
    "GraphDriver": {
      "Data": null,
      "Name": "overlay2"
    },
    "RootFS": {
      "Type": "layers",
      "Layers": [
        "sha256:2db29710123e5455b34e722505b562504b39e55355e344405b54d3153a94650b"
      ]
    },
    "Metadata": {
      "LastTagTime": "0001-01-01T00:00:00Z"
    }
  }
]

This default output provides a comprehensive view of the image's configuration and metadata. In the next steps, you will learn how to format this output to extract specific information.

Inspect the image with JSON format

In the previous step, you inspected the hello-world image and saw the default output format. While this format is informative, it can be difficult to parse programmatically. Docker's inspect command allows you to specify the output format using the --format flag. A common and useful format is JSON.

To inspect an image and get the output in JSON format, you use the --format json flag with the docker inspect command.

Let's inspect the hello-world image again, but this time, we will format the output as JSON.

docker inspect --format json hello-world

This command will output the same information as before, but it will be formatted as a JSON array containing a single JSON object.

[
  {
    "Id": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
    "RepoTags": ["hello-world:latest"],
    "RepoDigests": [
      "hello-world@sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591"
    ],
    "Parent": "",
    "Comment": "",
    "Created": "2023-03-07T21:50:09.004511104Z",
    "Container": "a928b81f51a91111111111111111111111111111111111111111111111111111",
    "ContainerConfig": {
      "Hostname": "a928b81f51a9",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": {}
    },
    "DockerVersion": "20.10.21",
    "Author": "",
    "Config": {
      "Hostname": "",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": null
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 1330,
    "VirtualSize": 1330,
    "GraphDriver": {
      "Data": null,
      "Name": "overlay2"
    },
    "RootFS": {
      "Type": "layers",
      "Layers": [
        "sha256:2db29710123e5455b34e722505b562504b39e55355e344405b54d3153a94650b"
      ]
    },
    "Metadata": {
      "LastTagTime": "0001-01-01T00:00:00Z"
    }
  }
]

JSON format is particularly useful when you want to process the output with other tools or scripts.

Inspect the image with a custom Go template

In addition to the default and JSON formats, the docker inspect command allows you to use a Go template to format the output. This is very powerful as it allows you to extract specific fields and format the output exactly as you need it.

The --format flag accepts a Go template string. You can access the fields of the inspected object using dot notation. For example, to access the image ID, you would use .Id. To access nested fields, you can chain the dots, like .Config.Cmd to get the command that the container will run.

Let's inspect the hello-world image and extract the image ID and the command that the container will execute.

docker inspect --format 'ID: {{.Id}} Command: {{.Config.Cmd}}' hello-world

In this command, we are using a Go template string 'ID: {{.Id}} Command: {{.Config.Cmd}}'.

  • {{.Id}} accesses the Id field of the image object.
  • {{.Config.Cmd}} accesses the Cmd field within the Config object.

The output will be formatted according to the template:

ID: sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591 Command: [/hello]

You can use Go templates to extract any information available in the default docker inspect output. This is very useful for scripting and automating tasks.

Summary

In this lab, you learned how to use the docker pull command to download a Docker image from a registry, specifically pulling the hello-world image as an example. You then explored the docker inspect command to view detailed information about the pulled image.

You practiced inspecting the image using the default output format, which provides a comprehensive overview of the image's characteristics. The subsequent steps, though not fully detailed in the provided content, would likely involve exploring how to format the docker inspect output using JSON and custom Go templates to extract specific information, demonstrating the flexibility of the command for analyzing image details.