当使用来自注册中心的docker映像时,我经常需要查看映像容器创建的卷。

注意:我在Red Hat 7上使用docker 1.3.2版本。

例子

Docker Registry的postgress官方镜像有一个为容器/var/lib/postgresql/data.配置的卷

在postgres容器中显示/var/lib/postgresql/data的卷最简洁的命令是什么?


当前回答

docker inspect -f '{{ json .Mounts }}' containerid | jq '.[]'

其他回答

如果您正在使用pwsh (powershell核心),可以尝试一下

(docker ps --format='{{json .}}' |  ConvertFrom-Json).Mounts

你也可以看到下面的容器名称和装载

docker ps --format='{{json .}}' |  ConvertFrom-Json | select Names,Mounts

当输出被转换为json时,您可以获得它所具有的任何属性。

我们可以不使用-f Go模板语法:

docker inspect <CONTAINER_ID> | jq .[].Mounts

第一个jq操作jq .[]将对象{}包装器剥离。

第二个jq操作将返回所有Mount项。

这是我的版本,找到一个码头撰写挂载点。 在使用此备份卷时。

 # for Id in $(docker-compose -f ~/ida/ida.yml ps -q); do docker inspect -f '{{ (index .Mounts 0).Source }}' $Id; done
/data/volumes/ida_odoo-db-data/_data
/data/volumes/ida_odoo-web-data/_data

这是以前解决方案的组合。

You can get information about which volumes were specifically baked into the container by inspecting the container and looking in the JSON output and comparing a couple of the fields. When you run docker inspect myContainer, the Volumes and VolumesRW fields give you information about ALL of the volumes mounted inside a container, including volumes mounted in both the Dockerfile with the VOLUME directive, and on the command line with the docker run -v command. However, you can isolate which volumes were mounted in the container using the docker run -v command by checking for the HostConfig.Binds field in the docker inspect JSON output. To clarify, this HostConfig.Binds field tells you which volumes were mounted specifically in your docker run command with the -v option. So if you cross-reference this field with the Volumes field, you will be able to determine which volumes were baked into the container using VOLUME directives in the Dockerfile.

grep可以像这样完成:

$ docker inspect myContainer | grep -C2 Binds
...
"HostConfig": {
    "Binds": [
        "/var/docker/docker-registry/config:/registry"
    ],

和…

$ docker inspect myContainer | grep -C3 -e "Volumes\":"
...
"Volumes": {
    "/data": "/var/lib/docker...",
    "/config": "/var/lib/docker...",
    "/registry": "/var/docker/docker-registry/config"

在我的例子中,你可以看到我使用docker run命令中的-v选项将/var/docker/docker-registry/config挂载到容器中作为/registry,并且我使用Dockerfile中的VOLUME指令挂载了/data和/config卷。容器不需要运行来获取这些信息,但它需要至少运行一次才能填充docker inspect命令的HostConfig JSON输出。

如果你想列出所有的容器名称和每个容器的相关卷,你可以尝试这样做:

docker ps -q | xargs docker container inspect -f '{{ .Name }} {{ .HostConfig.Binds }}'

示例输出:

/ opt_rundeck_1 [/ opt / var /李勃/ mysql: / var /李勃/ mysql: rw / var /李勃/ rundeck / var /储存:/ var /李勃/ rundeck / var /储存:rw / opt / var / rundeck / .ssh: / var /李勃/ rundeck / .ssh: rw / etc / / opt / etc / rundeck: rundeck: rw / var / log / rundeck: / var / log / rundeck: rw / opt / rundeck-plugins: / opt / rundeck-plugins: rw / opt / var / rundeck: / var / rundeck: rw]

/ opt_r十一k_1 -容器名称

(. .-附在容器上的卷