How to use docker volume inspect command to view volume details

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker volume inspect command to view detailed information about Docker volumes. We will begin by creating a Docker volume, which is the preferred method for persisting container data.

Following the creation, you will inspect the volume to see its full configuration details, including its name, driver, and the crucial mountpoint on the host machine. Finally, you will learn how to format the output of the inspect command to specifically display only the mountpoint, demonstrating how to extract specific information using Go templates.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/inspect -.-> lab-555259{{"How to use docker volume inspect command to view volume details"}} docker/create -.-> lab-555259{{"How to use docker volume inspect command to view volume details"}} docker/volume -.-> lab-555259{{"How to use docker volume inspect command to view volume details"}} end

Create a Docker volume

In this step, we will learn how to create a Docker volume. Docker volumes are the preferred way to persist data generated by and used by Docker containers. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker.

To create a Docker volume, we use the docker volume create command followed by the name you want to give the volume. Let's create a volume named myvolume.

docker volume create myvolume

You should see the name of the volume printed to the console if the creation was successful. This indicates that Docker has successfully created a storage volume named myvolume that can be used by containers.

Inspect the created volume

In the previous step, we created a Docker volume named myvolume. Now, let's inspect this volume to see its details. The docker volume inspect command provides detailed information about a specific volume, including its name, driver, and mountpoint on the host machine.

To inspect the myvolume volume, run the following command:

docker volume inspect myvolume

The output will be a JSON array containing information about the volume. You will see details like the volume's Name, Driver (which is usually local by default), Mountpoint, and other configurations. The Mountpoint is the directory on the host machine where the volume's data is stored.

Format the output to show the mountpoint

In the previous step, we inspected the myvolume volume and saw the full JSON output. Often, you might only be interested in specific pieces of information, like the mountpoint. Docker's inspect command allows you to format the output using Go templates.

To display only the mountpoint of the myvolume volume, we can use the -f or --format flag with a Go template. The template {{.Mountpoint}} will extract the value of the Mountpoint field from the JSON output.

Run the following command to display only the mountpoint:

docker volume inspect myvolume -f '{{.Mountpoint}}'

The output of this command will be the absolute path on the host machine where the myvolume data is stored. This path is managed by Docker and is typically located within the Docker data root directory.

Summary

In this lab, we learned how to manage Docker volumes, which are the preferred method for persisting container data. We began by creating a Docker volume named myvolume using the docker volume create command. This demonstrated the basic process of establishing a managed storage area for containers.

Following the creation, we explored how to view detailed information about the volume using the docker volume inspect command. This command provides a comprehensive JSON output containing crucial details like the volume's name, driver, and importantly, its mountpoint on the host machine. Finally, we learned how to format the output of the inspect command using Go templates, specifically focusing on extracting and displaying only the Mountpoint information, illustrating how to retrieve specific data points from the detailed volume information.