当使用来自注册中心的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.

其他回答

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

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

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

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

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

在docker 1.10中,现在有了用于数据量容器的新命令。 (对于常规容器,请参见下一节,对于docker 1.8+):

Docker卷ls 码头容积检查


在docker 1.8.1(2015年8月)中,docker inspect -f '{{.Volumes}}' containerid将为空!

你现在需要检查Mounts,这是一个挂载路径列表,比如:

   "Mounts": [
       {
           "Name": "7ced22ebb63b78823f71cf33f9a7e1915abe4595fcd4f067084f7c4e8cc1afa2",
           "Source": "/mnt/sda1/var/lib/docker/volumes/7ced22ebb63b78823f71cf33f9a7e1915abe4595fcd4f067084f7c4e8cc1afa2/_data",
           "Destination": "/home/git/repositories",
           "Driver": "local",
           "Mode": "",
           "RW": true
       }
   ],

如果你想要第一个挂载的路径(例如),这将是(使用索引0):

docker inspect -f '{{ (index .Mounts 0).Source }}' containerid

Mike Mitterer在下面评论道:

漂亮的打印整件事:

 docker inspect -f '{{ json .Mounts }}' containerid | python -m json.tool 

或者,正如Mitja所评论的那样,使用jq命令。

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

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

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

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

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

下面是一行命令来获取正在运行的容器的卷信息:

for contId in `docker ps -q`; do echo "Container Name: "   `docker ps -f "id=$contId" | awk '{print $NF}' | grep -v NAMES`; echo "Container Volume: " `docker inspect -f '{{.Config.Volumes}}' $contId`; docker inspect -f '{{ json .Mounts }}' $contId  | jq '.[]';   printf "\n"; done

输出是:

root@ubuntu:/var/lib# for contId in `docker ps -q`; do echo "Container Name: "   `docker ps -f "id=$contId" | awk '{print $NF}' | grep -v NAMES`; echo "Container Volume: " `docker inspect -f '{{.Config.Volumes}}' $contId`; docker inspect -f '{{ json .Mounts }}' $contId  | jq '.[]';   printf "\n"; done

Container Name:  freeradius
Container Volume:  map[]

Container Name:  postgresql
Container Volume:  map[/run/postgresql:{} /var/lib/postgresql:{}]
{
  "Propagation": "",
  "RW": true,
  "Mode": "",
  "Driver": "local",
  "Destination": "/run/postgresql",
  "Source":     "/var/lib/docker/volumes/83653a53315c693f0f31629f4680c56dfbf861c7ca7c5119e695f6f80ec29567/_data",
  "Name": "83653a53315c693f0f31629f4680c56dfbf861c7ca7c5119e695f6f80ec29567"
}
{
  "Propagation": "rprivate",
  "RW": true,
  "Mode": "",
  "Destination": "/var/lib/postgresql",
  "Source": "/srv/docker/postgresql"
}

Container Name:  rabbitmq
Container Volume:  map[]

码头工人版本:

root@ubuntu:~# docker version
Client:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:        Wed Oct 26 21:44:32 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:        Wed Oct 26 21:44:32 2016
 OS/Arch:      linux/amd64

在docker版本>= 1.8上使用单引号

docker inspect -f '{{ .Mounts }}' containerid

导致以下错误-

Template parsing error: template: :1: unclosed action

用双引号代替-

docker inspect -f "{{ .Mounts }}" <contained-id>