How to use docker inspect command to get detailed information

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker inspect command to retrieve detailed information about Docker objects, specifically focusing on containers. You will begin by inspecting a container and understanding the comprehensive default JSON output provided by the command.

Building upon this, you will then explore how to format the output using Go templating to extract specific data points, such as a container's IP address. The lab will also cover how to inspect the size of a container and demonstrate how to inspect different types of Docker objects beyond just containers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/run -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} docker/ps -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} docker/inspect -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} docker/pull -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} docker/images -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} docker/volume -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} docker/network -.-> lab-555165{{"How to use docker inspect command to get detailed information"}} end

Inspect a container and understand the default output

In this step, you will learn how to inspect a Docker container and understand the default output. The docker inspect command provides detailed information about Docker objects, including containers, images, networks, and volumes. By default, it returns a JSON array containing configuration details, state, and other metadata.

First, let's pull a simple image to work with. We will use the hello-world image.

docker pull hello-world

You should see output indicating that the image is being pulled and extracted.

Now, let's run a container from this image.

docker run hello-world

This command runs the hello-world container, which prints a message and then exits.

To inspect the container that just ran, you need its ID or name. You can find the last exited container's ID using docker ps -a.

docker ps -a

Look for the container with the hello-world image and copy its CONTAINER ID.

Now, use the docker inspect command followed by the container ID you just copied. Replace <container_id> with the actual ID.

docker inspect <container_id>

This command will output a large JSON document containing detailed information about the container. This default output includes a wide range of data, such as the container's state, configuration, network settings, and mounted volumes. Understanding this output is crucial for debugging and managing containers.

Scroll through the output to get a sense of the information available. You will see fields like Id, State, Created, Path, Args, Config, NetworkSettings, and many more.

Format the output to get specific information

In the previous step, you saw that docker inspect provides a large amount of information in JSON format. Often, you only need specific pieces of information. Docker allows you to format the output using the --format flag and Go templating.

Let's get the IP address of a running container. First, we need a running container. We will use a simple nginx container for this.

Pull the nginx image:

docker pull nginx

Run an nginx container in detached mode (-d) so it runs in the background:

docker run -d nginx

This command will output the container ID. Copy this ID.

Now, let's use docker inspect with the --format flag to get the container's IP address. Replace <container_id> with the ID of your running nginx container.

docker inspect --format='{{.NetworkSettings.IPAddress}}' <container_id>

The --format flag uses Go templating syntax. . refers to the top-level JSON object. We navigate through the JSON structure using dot notation: .NetworkSettings accesses the NetworkSettings object, and .IPAddress accesses the IPAddress field within NetworkSettings. The double curly braces {{ }} indicate a Go template action.

You should see the IP address of the container printed to the console.

You can extract other information similarly. For example, to get the container's state:

docker inspect --format='{{.State.Status}}' <container_id>

This will output the current status of the container (e.g., "running").

To get the container's name:

docker inspect --format='{{.Name}}' <container_id>

This will output the name of the container (e.g., /vigilant_goldberg).

You can also combine multiple fields in the format string. For example, to get the container ID and its status:

docker inspect --format='ID: {{.Id}}, Status: {{.State.Status}}' <container_id>

This will print the ID and status in a more readable format.

Experiment with different fields from the JSON output you saw in the previous step to extract the information you need.

Inspect the size of a container

In this step, you will learn how to inspect the size of a Docker container. Understanding container size is important for managing disk space and optimizing image builds.

The docker inspect command can provide information about the container's size, but a more direct way to see the size of running containers is using the docker ps command with the --size flag.

First, ensure you have a running container. If your nginx container from the previous step is still running, you can use that. If not, run it again:

docker run -d nginx

Now, list the running containers and include their size information:

docker ps --size

You will see an additional column labeled SIZE. This column shows two values: the virtual size and the size on disk.

  • Virtual Size: This is the total size of the container's image layers plus the size of the container's writable layer.
  • Size: This is the size of the container's writable layer on disk. This layer contains all the changes made to the container's filesystem since it was started.

For containers that have exited, you can use docker ps -a --size to see their sizes.

docker ps -a --size

You can also get size information using docker inspect and formatting the output. The size information is available under .SizeRw (size of the writable layer) and .SizeRootFs (total size including image layers).

Replace <container_id> with the ID of one of your containers:

docker inspect --format='Writable Layer Size: {{.SizeRw}}, Total Size: {{.SizeRootFs}}' <container_id>

This command will output the size of the writable layer and the total size of the container's root filesystem.

Understanding the difference between virtual size and the size on disk is important. The virtual size includes the shared image layers, while the size on disk for a running container primarily reflects the unique data written to that specific container's writable layer.

Inspect a specific type of object

In the previous steps, you used docker inspect on containers. However, docker inspect is a versatile command that can be used to inspect various Docker objects, including images, networks, and volumes.

To inspect an object other than a container, you need to specify the object type before the object's name or ID. The syntax is docker inspect <object_type> <object_name_or_id>.

Let's inspect the nginx image you pulled earlier.

First, list your images to get the image ID or name:

docker images

Find the nginx image and note its REPOSITORY, TAG, or IMAGE ID.

Now, inspect the nginx image. You can use the image name and tag (e.g., nginx:latest) or the image ID. Replace <image_name_or_id> with the appropriate value.

docker inspect image <image_name_or_id>

This will output detailed information about the nginx image, including its layers, configuration, and metadata. Notice that the structure of the output is different from inspecting a container, as it reflects the properties of an image.

Next, let's inspect a Docker network. Docker creates default networks. You can list them using docker network ls.

docker network ls

You will likely see networks like bridge, host, and none. Let's inspect the bridge network.

docker inspect network bridge

This command will show details about the bridge network, including its configuration, connected containers, and IP addressing information.

Finally, let's inspect a Docker volume. If you don't have any volumes, you can create one.

docker volume create myvolume

Now, inspect the volume you just created.

docker inspect volume myvolume

This will display information about the myvolume, such as its driver, mount point, and scope.

By specifying the object type (container, image, network, volume, etc.), you can use docker inspect to get detailed information about different components of your Docker environment.

Summary

In this lab, you learned how to use the docker inspect command to obtain detailed information about Docker containers. You began by inspecting a container and understanding the comprehensive default JSON output, which provides a wide range of configuration and state details.

Subsequently, you explored how to format the output using the --format flag and Go templating to extract specific information, such as a container's IP address. This demonstrates the flexibility of docker inspect in tailoring the output to your needs.