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

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

例子

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

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


当前回答

对docker-compose用户有用的变体:

docker-compose ps -q | xargs docker container inspect  \
   -f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}' 

这将非常整洁地输出可解析的音量信息。例子来自我的wordpress docker-compose:

ubuntu@core $ docker-compose ps -q | xargs docker container inspect  -f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}' 
core_wpdb:/var/lib/mysql 
core_wpcode:/code core_wphtml:/var/www/html 

每个容器的输出都包含一行,列出了使用的卷(和挂载点)。修改{{. name}}:{{. destination}}部分来输出你想要的信息。

如果您只想要一个简单的卷列表,则每行一个

$ docker-compose ps -q | xargs docker container inspect  \
   -f '{{ range .Mounts }}{{ .Name }} {{ end }}' \
   | xargs -n 1 echo
core_wpdb
core_wpcode
core_wphtml

Great to generate a list of volumes to backup. I use this technique along with Blacklabelops Volumerize to backup all volumes used by all containers within a docker-compose. The docs for Volumerize don't call it out, but you don't need to use it in a persistent container or to use the built-in facilities for starting and stopping services. I prefer to leave critical operations such as backup and service control to the actual user (outside docker). My backups are triggered by the actual (non-docker) user account, and use docker-compose stop to stop services, backup all volumes in use, and finally docker-compose start to restart.

其他回答

从Docker文档这里

.Mounts Names of the volumes mounted in this container.

码头工人ps————no-trunc格式”{{.ID}} {{. name}} \ \ t t {{.Mounts}}”

应该工作

对docker-compose用户有用的变体:

docker-compose ps -q | xargs docker container inspect  \
   -f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}' 

这将非常整洁地输出可解析的音量信息。例子来自我的wordpress docker-compose:

ubuntu@core $ docker-compose ps -q | xargs docker container inspect  -f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}' 
core_wpdb:/var/lib/mysql 
core_wpcode:/code core_wphtml:/var/www/html 

每个容器的输出都包含一行,列出了使用的卷(和挂载点)。修改{{. name}}:{{. destination}}部分来输出你想要的信息。

如果您只想要一个简单的卷列表,则每行一个

$ docker-compose ps -q | xargs docker container inspect  \
   -f '{{ range .Mounts }}{{ .Name }} {{ end }}' \
   | xargs -n 1 echo
core_wpdb
core_wpcode
core_wphtml

Great to generate a list of volumes to backup. I use this technique along with Blacklabelops Volumerize to backup all volumes used by all containers within a docker-compose. The docs for Volumerize don't call it out, but you don't need to use it in a persistent container or to use the built-in facilities for starting and stopping services. I prefer to leave critical operations such as backup and service control to the actual user (outside docker). My backups are triggered by the actual (non-docker) user account, and use docker-compose stop to stop services, backup all volumes in use, and finally docker-compose start to restart.

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

 # 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

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

我在谷歌上搜了一下,找到了我自己的答案:)我最近的记忆力……对于那些不了解它的人来说,commandlinefu是一个找到并发布这些代码片段的好地方。

按容器列出docker卷。

docker ps -a --format '{{ .ID }}' | xargs -I {} docker inspect -f '{{ .Name }}{{ printf "\n" }}{{ range .Mounts }}{{ printf "\n\t" }}{{ .Type }} {{ if eq .Type "bind" }}{{ .Source }}{{ end }}{{ .Name }} => {{ .Destination }}{{ end }}{{ printf "\n" }}' {}

示例输出。

root@jac007-truserv-jhb1-001 ~/gitlab $ docker ps -a --format '{{ .ID }}' | xargs -I {} docker inspect -f '{{ .Name }}{{ printf "\n" }}{{ range .Mounts }}{{ printf "\n\t" }}{{ .Type }} {{ if eq .Type "bind" }}{{ .Source }}{{ end }}{{ .Name }} => {{ .Destination }}{{ end }}{{ printf "\n" }}' {}
/gitlab_server_1

    volume gitlab-data => /var/opt/gitlab
    volume gitlab-config => /etc/gitlab
    volume gitlab-logs => /var/log/gitlab

/gitlab_runner_1

    bind /var/run/docker.sock => /var/run/docker.sock
    volume gitlab-runner-config => /etc/gitlab-runner
    volume 35b5ea874432f55a26c769e1cdb1ee3f06f78759e6f302e3c4b4aa40f3a495aa => /home/gitlab-runner

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

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

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

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

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