Listing Docker Volumes – 列出Docker卷

最后修改: 2022年 6月 12日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Docker is one of the most popular container technologies. It packages an application with its dependencies and allows it to run in an isolated environment. This technique greatly increases an application’s portability. By default, container storage is ephemeral, which is not ideal for stateful applications. However, we can use volumes to overcome this limitation.

Docker是最流行的容器技术之一。它将应用程序与它的依赖关系打包,并允许它在一个隔离的环境中运行。这种技术大大增加了应用程序的可移植性。默认情况下,容器存储是短暂的,这对于有状态的应用程序来说并不理想。然而,我们可以使用volumes来克服这一限制。

In this tutorial, we’ll see how to list volumes and show detailed information about them.

在本教程中,我们将看到如何列出卷,并显示关于它们的详细信息。

2. Setting up an Example

2.设置一个例子

Let’s create a few volumes with attributes to use as an example:

让我们创建几个带有属性的卷,作为一个例子。

$ docker volume create dangling-volume
$ docker volume create narendra-volume --driver local --opt type=tmpfs --opt device=tmpfs
$ docker volume create labeled-volume --label owner=narendra

The latter section of the tutorial shows how we can use these attributes to filter volumes. In the next section, we’ll see how to verify our example has been set up correctly.

本教程的后一节显示了我们如何使用这些属性来过滤卷。在下一节中,我们将看到如何验证我们的例子已经被正确设置了。

3. Displaying Basic Volume Information

3.显示音量的基本信息

Docker’s volume list child command will display a brief summary of each volume:

Docker的volume list子命令将显示每个卷的简要摘要

$ docker volume list
DRIVER    VOLUME NAME
local     dangling-volume
local     labeled-volume
local     narendra-volume

Sometimes, we only need the volume names. In such scenarios, we can use the –quiet option:

有时,我们只需要卷的名称。在这种情况下,我们可以使用-quiet选项。

$ docker volume list --quiet
dangling-volume
labeled-volume
narendra-volume

The above approaches work fine for a small handful of volumes but can be tedious when there are many volumes to list. In such cases, we can use the –filter option. This option allows us to perform filtering based on certain attributes. Let’s look at a few examples.

上述方法对一小部分卷的工作很好,但当有很多卷需要列出时,可能会很繁琐。在这种情况下,我们可以使用-filter选项。这个选项允许我们根据某些属性进行过滤。让我们看几个例子。

3.1. Filtering on Name

3.1.名称上过滤

We can use the name filter to list the volume whose name contains a certain string. Let’s list the volume whose name contains “narendra”:

我们可以使用name过滤器来列出名称中包含某个字符串的卷。让我们列出名称中包含 “narendra “的卷。

$ docker volume list --filter name=narendra
DRIVER    VOLUME NAME
local     narendra-volume

3.2. Filtering on Labels

3.2.标签的过滤

Labels are used to tag resources. A very common scenario is to group the resources that match certain criteria. For example, developers can use their usernames as a label so they can easily identify the volumes they’ve created themselves. Let’s understand this with an example:

标签是用来标记资源的。一个非常常见的情况是将符合某些标准的资源分组。例如,开发人员可以使用他们的用户名作为标签,这样他们就可以很容易地识别他们自己创建的卷。让我们通过一个例子来理解这个问题。

$ docker volume list --filter label=owner=narendra
DRIVER    VOLUME NAME
local     labeled-volume

In this example, the filter matches volumes with the label owner=narendra, which we added when setting up our example volumes.

在这个例子中,过滤器匹配标签为owner=narendra的卷,这是我们在设置示例卷时添加的。

3.3. Filtering on the Driver’s Name

3.3.过滤 根据驱动程序的名称进行过滤

Sometimes, we need to segregate volumes based on their driver’s name. In such cases, we can use the driver filter:

有时,我们需要根据驱动器的名称来隔离卷。在这种情况下,我们可以使用driver过滤器。

$ docker volume list --filter driver=local
DRIVER    VOLUME NAME
local     dangling-volume
local     labeled-volume
local     narendra-volume

3.4. Dangling Volumes

3.4 悬空的体积

Volumes take up space on the docker host, so it’s a good practice to clean up any unused volumes. Because removing the wrong volume can cause data loss, we have to be extra careful before deleting them. As a safety check, we can ensure a volume is not referenced by any container by using the dangling filter. Let’s see this in action.

卷占用了docker主机的空间,所以清理任何未使用的卷是一个好的做法。因为删除错误的卷可能会导致数据丢失,所以我们在删除它们之前必须要格外小心。作为安全检查,我们可以通过使用dangling过滤器来确保卷没有被任何容器引用。让我们看看这个方法的作用。

First, let’s create a container that uses volumes:

首先,让我们创建一个使用卷的容器。

$ docker container run -d --name dangling-volume-demo -v narendra-volume:/tmpwork \
   -v labeled-volume:/data busybox
fa3f6fd8261293a92da7efbca4b04040a1838cf57b2703795324eb70a3d84143

In this example, the container is using two of our three volumes: narendra-volume and labeled-volume. Let’s now confirm that the third volume is the only one that appears as dangling/unused:

在这个例子中,容器正在使用我们三个卷中的两个。narendra-volumelabeled-volume。现在让我们确认一下,第三个卷是唯一一个显示为悬空/未使用的卷。

$ docker volume list --filter dangling=true
DRIVER    VOLUME NAME
local     dangling-volume

4. Displaying Detailed Volume Information

4.显示详细的体积信息

The list child command shows very limited information about volumes. Sometimes this is not enough. For example, debugging is much easier if we know the details of a volume. In such cases, we can use the volume inspect child command to get additional information about a volume. This command shows information like the volume’s creation timestamp, mount point, etc. Let’s see this with an example:

list子命令显示关于卷的非常有限的信息。有时这还不够。例如,如果我们知道一个卷的细节,调试会更容易。在这种情况下,我们可以使用volume inspect子命令来获得关于卷的额外信息。这个命令显示了诸如卷的创建时间戳、挂载点等信息。让我们通过一个例子来看看。

$ docker volume inspect labeled-volume
[
    {
        "CreatedAt": "2022-05-30T22:34:53+05:30",
        "Driver": "local",
        "Labels": {
            "owner": "narendra"
        },
        "Mountpoint": "/var/lib/docker/volumes/labeled-volume/_data",
        "Name": "labeled-volume",
        "Options": {},
        "Scope": "local"
    }
]

5. Displaying Container-Specific Volume Information

5.显示容器的具体体积信息

Another very common scenario is to find the volumes used by a given container. Developers often need this information to debug the application. We can get information about a specific contain’s volume(s) using the container inspect child command. This command returns low-level information on Docker objects, such as their state, host configuration, network setting, etc. We can specify the Mounts section to gather mount information about a volume:

另一个非常常见的情况是找到一个给定容器所使用的卷。开发人员经常需要这些信息来调试应用程序。我们可以使用container inspect子命令来获取特定容器的卷的信息。该命令返回Docker对象的低级信息,如它们的状态、主机配置、网络设置等。我们可以指定Mounts部分来收集一个卷的挂载信息。

$ docker container inspect --format '{{ json .Mounts }}' dangling-volume-demo | python3 -m json.tool
[
    {
        "Type": "volume",
        "Name": "narendra-volume",
        "Source": "/var/lib/docker/volumes/narendra-volume/_data",
        "Destination": "/tmpwork",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    },
    {
        "Type": "volume",
        "Name": "labeled-volume",
        "Source": "/var/lib/docker/volumes/labeled-volume/_data",
        "Destination": "/data",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    }
]

Note that, in this example, we’ve piped output to the Python interpreter in order to make the JSON output easier to read. However, this is completely optional.

请注意,在这个例子中,为了使JSON输出更容易阅读,我们已经将输出引向Python解释器。然而,这完全是可选的。

6. Conclusion

6.结语

In this article, we saw some practical examples of listing docker volumes. First, we used the list child command. Then we saw how to use filters with it. Finally, we used the inspect child command to show detailed information about the volumes.

在这篇文章中,我们看到了一些列出docker卷的实际例子。首先,我们使用了list子命令。然后我们看到了如何使用filters。最后,我们使用inspect子命令来显示卷的详细信息。