When using docker images from registries, I often need to see the volumes created by the image's containers.
使用来自注册表的docker镜像时,我经常需要查看图像容器创建的卷。
Note: I'm using docker version 1.3.2 on Red Hat 7.
注意:我在Red Hat 7上使用docker 1.3.2版。
Example
The postgres
official image from the Docker Registry has a volume configured for containers at /var/lib/postgresql/data
.
来自Docker Registry的postgres官方图像在/ var / lib / postgresql / data中为容器配置了一个卷。
What's the most succinct command to show the volume at /var/lib/postgresql/data
in a postgres
container?
在postgres容器中显示/ var / lib / postgresql / data卷的最简洁命令是什么?
10 个解决方案
#1
Use docker ps
to get the container id.
使用docker ps获取容器ID。
Then docker inspect -f '{{ .Mounts }}' containerid
然后docker检查-f'{{.Mounts}}'containerid
Example:
terminal 1
$ docker run -it -v /tmp:/tmp ubuntu:14.04 /bin/bash
terminal 2
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddb7b55902cc ubuntu:14.04 "/bin/bash" About a minute ago Up About a minute distracted_banach
$ docker inspect -f "{{ .Mounts }}" ddb7
map[/tmp:/tmp]
The output
map[/tmp:/tmp]
is, apparently, due to the use of the Go language to implement the docker command tools.
显然,由于使用Go语言来实现docker命令工具。
The docker inspect
command without the -f format
is quite verbose. Since it is JSON you could pipe it to python or nodejs and extract whatever you needed.
没有-f格式的docker inspect命令非常详细。因为它是JSON,你可以将它传递给python或nodejs并提取你需要的任何东西。
paul@home:~$ docker inspect ddb7
[{
"AppArmorProfile": "",
"Args": [],
"Config": {
"AttachStderr": true,
"AttachStdin": true,
"AttachStdout": true,
"Cmd": [
"/bin/bash"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": null,
"Hostname": "ddb7b55902cc",
"Image": "ubuntu:14.04",
"MacAddress": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": true,
"PortSpecs": null,
"StdinOnce": true,
"Tty": true,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Created": "2015-05-08T22:41:44.74862921Z",
"Driver": "devicemapper",
"ExecDriver": "native-0.2",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/tmp:/tmp"
],
"CapAdd": null,
"CapDrop": null,
"ContainerIDFile": "",
"Devices": [],
"Dns": null,
"DnsSearch": null,
"ExtraHosts": null,
"IpcMode": "",
"Links": null,
"LxcConf": [],
"NetworkMode": "bridge",
"PidMode": "",
"PortBindings": {},
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"RestartPolicy": {
"MaximumRetryCount": 0,
"Name": ""
},
"SecurityOpt": null,
"VolumesFrom": null
},
"HostnamePath": "/var/lib/docker/containers/ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a/hostname",
"HostsPath": "/var/lib/docker/containers/ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a/hosts",
"Id": "ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a",
"Image": "ed5a78b7b42bde1e3e4c2996e02da778882dca78f8919cbd0deb6694803edec3",
"MountLabel": "",
"Name": "/distracted_banach",
"NetworkSettings": {
"Bridge": "docker0",
"Gateway": "172.17.42.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "fe80::42:acff:fe11:4",
"LinkLocalIPv6PrefixLen": 64,
"MacAddress": "02:42:ac:11:00:04",
"PortMapping": null,
"Ports": {}
},
"Path": "/bin/bash",
"ProcessLabel": "",
"ResolvConfPath": "/var/lib/docker/containers/ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a/resolv.conf",
"RestartCount": 0,
"State": {
"Error": "",
"ExitCode": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"OOMKilled": false,
"Paused": false,
"Pid": 6115,
"Restarting": false,
"Running": true,
"StartedAt": "2015-05-08T22:41:45.367432585Z"
},
"Volumes": {
"/tmp": "/tmp"
},
"VolumesRW": {
"/tmp": true
}
}
]
docker history <image name>
will show the layers baked into an image. Unfortunately, docker history
seems hobbled by its formatting and lack of options to choose what is displayed.
docker history 将显示烘焙到图像中的图层。不幸的是,泊坞历史似乎因格式化而缺乏选择显示内容的选项。
You can choose terse and verbose formats, via the --no-trunc flag.
您可以通过--no-trunc标志选择简洁和详细格式。
$ docker history drpaulbrewer/spark-worker
IMAGE CREATED CREATED BY SIZE
438ff4e1753a 2 weeks ago /bin/sh -c #(nop) CMD [/bin/sh -c /spark/my-s 0 B
6b664e299724 2 weeks ago /bin/sh -c #(nop) ADD file:09da603c5f0dca7cc6 296 B
f6ae126ae124 2 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaf 0 B
70bcb3ffaec9 2 weeks ago /bin/sh -c #(nop) EXPOSE 2222/tcp 4040/tcp 60 0 B
1332ac203849 2 weeks ago /bin/sh -c apt-get update && apt-get --yes up 1.481 GB
8e6f1e0bb1b0 2 weeks ago /bin/sh -c sed -e 's/archive.ubuntu.com/www.g 1.975 kB
b3d242776b1f 2 weeks ago /bin/sh -c #(nop) WORKDIR /spark/spark-1.3.1 0 B
ac0d6cc5aa3f 2 weeks ago /bin/sh -c #(nop) ADD file:b6549e3d28e2d149c0 25.89 MB
6ee404a44b3f 5 weeks ago /bin/sh -c #(nop) WORKDIR /spark 0 B
c167faff18cf 5 weeks ago /bin/sh -c adduser --disabled-password --home 335.1 kB
f55d468318a4 5 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaf 0 B
19c8c047d0fe 8 weeks ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B
c44d976a473f 8 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 1.879 kB
14dbf1d35e28 8 weeks ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic 701 B
afa7a164a0d2 8 weeks ago /bin/sh -c #(nop) ADD file:57f97478006b988c0c 131.5 MB
511136ea3c5a 23 months ago 0 B
Here's a verbose example.
这是一个冗长的例子。
docker history --no-trunc=true drpaulbrewer/spark-worker
IMAGE CREATED CREATED BY SIZE
438ff4e1753a60779f389a3de593d41f7d24a61da6e1df76dded74a688febd64 2 weeks ago /bin/sh -c #(nop) CMD [/bin/sh -c /spark/my-spark-worker.sh] 0 B
6b664e29972481b8d6d47f98167f110609d9599f48001c3ca11c22364196c98a 2 weeks ago /bin/sh -c #(nop) ADD file:09da603c5f0dca7cc60f1911caf30c3c70df5e4783f7eb10468e70df66e2109f in /spark/ 296 B
f6ae126ae124ca211c04a1257510930b37ea78425e31a273ea0b1495fa176c57 2 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaftc.com 0 B
70bcb3ffaec97a0d14e93b170ed70cc7d68c3c9dfb0222c1d360a300d6e05255 2 weeks ago /bin/sh -c #(nop) EXPOSE 2222/tcp 4040/tcp 6066/tcp 7077/tcp 7777/tcp 8080/tcp 8081/tcp 0 B
1332ac20384947fe1f15107213b675e5be36a68d72f0e81153d6d5a21acf35af 2 weeks ago /bin/sh -c apt-get update && apt-get --yes upgrade && apt-get --yes install sed nano curl wget openjdk-8-jdk scala && echo "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64" >>/etc/environment && export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m" && ./build/mvn -Phive -Phive-thriftserver -DskipTests clean package && chown -R spark:spark /spark && mkdir /var/run/sshd 1.481 GB
8e6f1e0bb1b0b9286947d3a4b443cc8099b00f9670aab1d58654051e06f62e51 2 weeks ago /bin/sh -c sed -e 's/archive.ubuntu.com/www.gtlib.gatech.edu\/pub/' /etc/apt/sources.list > /tmp/sources.list && mv /tmp/sources.list /etc/apt/sources.list 1.975 kB
b3d242776b1f1f1ae5685471d06a91a68f92845ef6fc6445d831835cd55e5d0b 2 weeks ago /bin/sh -c #(nop) WORKDIR /spark/spark-1.3.1 0 B
ac0d6cc5aa3fdc3b65fc0173f6775af283c3c395c8dae945cf23940435f2785d 2 weeks ago /bin/sh -c #(nop) ADD file:b6549e3d28e2d149c0bc84f69eb0beab16f62780fc4889bcc64cfc9ce9f762d6 in /spark/ 25.89 MB
6ee404a44b3fdd3ef3318dc10f3d002f1995eea238c78f4eeb9733d00bb29404 5 weeks ago /bin/sh -c #(nop) WORKDIR /spark 0 B
c167faff18cfecedef30343ef1cb54aca45f4ef0478a3f6296746683f69d601b 5 weeks ago /bin/sh -c adduser --disabled-password --home /spark spark 335.1 kB
f55d468318a4778733160d377c5d350dc8f593683009699c2af85244471b15a3 5 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaftc.com 0 B
19c8c047d0fe2de7239120f2b5c1a20bbbcb4d3eb9cbf0efa59ab27ab047377a 8 weeks ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B
c44d976a473f143937ef91449c73f2cabd109b540f6edf54facb9bc2b4fff136 8 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list 1.879 kB
14dbf1d35e2849a00c6c2628055030fa84b4fb55eaadbe0ecad8b82df65cc0db 8 weeks ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/policy-rc.d && echo 'exit 101' >> /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d && dpkg-divert --local --rename --add /sbin/initctl && cp -a /usr/sbin/policy-rc.d /sbin/initctl && sed -i 's/^exit.*/exit 0/' /sbin/initctl && echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup && echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean && echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean && echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean && echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes 701 B
afa7a164a0d215dbf45cd1aadad2a4d12b8e33fc890064568cc2ea6d42ef9b3c 8 weeks ago /bin/sh -c #(nop) ADD file:57f97478006b988c0c68e5bf82684372e427fd45f21cd7baf5d974d2cfb29e65 in / 131.5 MB
511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158 23 months ago 0 B
#2
With docker 1.10, you now have new commands for data-volume containers.
(for regular containers, see the next section, for docker 1.8+):
使用docker 1.10,您现在可以使用新的数据卷容器命令。 (对于常规容器,请参阅下一节,对于docker 1.8+):
docker volume ls
-
docker volume inspect
码头工人量ls
码头工量检查
With docker 1.8.1 (August 2015), a docker inspect -f '{{ .Volumes }}' containerid
would be empty!
使用docker 1.8.1(2015年8月),docker inspect -f'{{.Volumes}}'containerid将为空!
You now need to check Mounts
, which is a list of mounted paths like:
您现在需要检查Mounts,这是一个已安装路径的列表,如:
"Mounts": [
{
"Name": "7ced22ebb63b78823f71cf33f9a7e1915abe4595fcd4f067084f7c4e8cc1afa2",
"Source": "/mnt/sda1/var/lib/docker/volumes/7ced22ebb63b78823f71cf33f9a7e1915abe4595fcd4f067084f7c4e8cc1afa2/_data",
"Destination": "/home/git/repositories",
"Driver": "local",
"Mode": "",
"RW": true
}
],
If you want the path of the first mount (for instance), that would be (using index 0):
如果你想要第一个mount(例如)的路径,那就是(使用索引0):
docker inspect -f '{{ (index .Mounts 0).Source }}' containerid
As Mike Mitterer comments below:
正如Mike Mitterer在下面评论:
Pretty print the whole thing:
漂亮打印整件事:
docker inspect -f '{{ json .Mounts }}' containerid | python -m json.tool
Or, as commented by Mitja, use the jq
command.
或者,如Mitja所评论的那样,使用jq命令。
docker inspect -f '{{ json .Mounts }}' containerid | jq
#3
Show names and mount point destinations of volumes used by a container:
显示容器使用的卷的名称和装入点目标:
docker container inspect \
-f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}' \
CONTAINER_ID_OR_NAME
This is compatible with Docker 1.13.
这与Docker 1.13兼容。
#4
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.
您可以通过检查容器并查看JSON输出并比较几个字段来获取有关哪些卷专门烘焙到容器中的信息。当您运行docker inspect myContainer时,Volumes和VolumesRW字段会为您提供有关容器内安装的所有卷的信息,包括使用VOLUME指令在Dockerfile中安装的卷,以及使用docker run -v命令在命令行上安装的卷。但是,您可以通过检查docker中的HostConfig.Binds字段检查JSON输出,使用docker run -v命令隔离容器中装入的卷。为了澄清,此HostConfig.Binds字段告诉您使用-v选项在docker run命令中专门装入了哪些卷。因此,如果您使用Volumes字段交叉引用此字段,则可以使用Dockerfile中的VOLUME指令确定将哪些卷烘焙到容器中。
A grep could accomplish this like:
grep可以实现这一点:
$ docker inspect myContainer | grep -C2 Binds
...
"HostConfig": {
"Binds": [
"/var/docker/docker-registry/config:/registry"
],
And...
$ docker inspect myContainer | grep -C3 -e "Volumes\":"
...
"Volumes": {
"/data": "/var/lib/docker...",
"/config": "/var/lib/docker...",
"/registry": "/var/docker/docker-registry/config"
And in my example, you can see I've mounted /var/docker/docker-registry/config
into the container as /registry
using the -v
option in my docker run
command, and I've mounted the /data
and /config
volumes using the VOLUME
directive in my Dockerfile. The container does not need to be running to get this information, but it needs to have been run at least one time in order to populate the HostConfig
JSON output of your docker inspect
command.
在我的例子中,您可以看到我已经使用我的docker run命令中的-v选项将/ var / docker / docker-registry / config作为/ registry挂载到容器中,并且我已经挂载了/ data和/ config我的Dockerfile中使用VOLUME指令的卷。容器不需要运行来获取此信息,但它需要至少运行一次才能填充docker inspect命令的HostConfig JSON输出。
#5
For Docker 1.8, I use:
对于Docker 1.8,我使用:
$ docker inspect -f "{{ .Config.Volumes }}" 957d2dd1d4e8
map[/xmount/dvol.01:{}]
$
#6
Here is one line command to get the volume information for running containers:
以下是一行命令,用于获取运行容器的卷信息:
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
Output is:
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[]
Docker version:
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
#7
Useful variation for docker-compose users:
docker-compose用户的有用变体:
docker-compose ps -q | xargs docker container inspect \
-f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}'
This will very neatly output parseable volume info. Example from my wordpress docker-compose:
这将非常整齐地输出可解析的音量信息。我的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
The output contains one line for each container, listing the volumes (and mount points) used. Alter the {{ .Name }}:{{ .Destination }} portion to output the info you would like.
输出包含每个容器的一行,列出使用的卷(和挂载点)。更改{{.Name}}:{{.Destination}}部分以输出您想要的信息。
If you just want a simple list of volumes, one per line
如果您只想要一个简单的卷列表,每行一个
$ 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.
非常适合生成要备份的卷列表。我使用这种技术和Blacklabelops Volumerize来备份docker-compose中所有容器使用的所有卷。 Volumerize的文档不会调用它,但您不需要在持久容器中使用它,也不需要使用内置工具来启动和停止服务。我更喜欢将关键操作(如备份和服务控制)留给实际用户(docker之外)。我的备份由实际(非docker)用户帐户触发,并使用docker-compose stop来停止服务,备份所有正在使用的卷,最后docker-compose start to restart。
#8
if you want to list all the containers name with the relevant volumes that attached to each container you can try this:
如果要列出所有容器名称以及附加到每个容器的相关卷,可以尝试:
docker ps -q | xargs docker container inspect -f '{{ .Name }} {{ .HostConfig.Binds }}'
example output:
/opt_rundeck_1 [/opt/var/lib/mysql:/var/lib/mysql:rw /var/lib/rundeck/var/storage:/var/lib/rundeck/var/storage:rw /opt/var/rundeck/.ssh:/var/lib/rundeck/.ssh:rw /opt/etc/rundeck:/etc/rundeck:rw /var/log/rundeck:/var/log/rundeck:rw /opt/rundeck-plugins:/opt/rundeck-plugins:rw /opt/var/rundeck:/var/rundeck:rw]
/ opt_rundeck_1 [/ opt / var / lib / mysql:/ var / lib / mysql:rw / var / lib / rundeck / var / storage:/ var / lib / rundeck / var / storage:rw / opt / var / rundeck / .ssh:/var/lib/rundeck/.ssh:rw / opt / etc / rundeck:/ etc / rundeck:rw / var / log / rundeck:/ var / log / rundeck:rw / opt / rundeck-plugins:/ opt / rundeck-plugins:rw / opt / var / rundeck:/ var / rundeck:rw]
/opt_rundeck_1 - container name
/ opt_rundeck_1 - 容器名称
[..] - volumes attached to the conatiner
[..] - 附在容器上的卷
#9
docker inspect -f '{{ json .Mounts }}' containerid | jq '.[]'
#10
Here is my version to find mount points of a docker compose. In use this to backup the volumes.
这是我的版本,用于查找docker compose的挂载点。在使用它来备份卷。
# 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
This is a combination of previous solutions.
这是以前解决方案的组合。
#1
Use docker ps
to get the container id.
使用docker ps获取容器ID。
Then docker inspect -f '{{ .Mounts }}' containerid
然后docker检查-f'{{.Mounts}}'containerid
Example:
terminal 1
$ docker run -it -v /tmp:/tmp ubuntu:14.04 /bin/bash
terminal 2
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddb7b55902cc ubuntu:14.04 "/bin/bash" About a minute ago Up About a minute distracted_banach
$ docker inspect -f "{{ .Mounts }}" ddb7
map[/tmp:/tmp]
The output
map[/tmp:/tmp]
is, apparently, due to the use of the Go language to implement the docker command tools.
显然,由于使用Go语言来实现docker命令工具。
The docker inspect
command without the -f format
is quite verbose. Since it is JSON you could pipe it to python or nodejs and extract whatever you needed.
没有-f格式的docker inspect命令非常详细。因为它是JSON,你可以将它传递给python或nodejs并提取你需要的任何东西。
paul@home:~$ docker inspect ddb7
[{
"AppArmorProfile": "",
"Args": [],
"Config": {
"AttachStderr": true,
"AttachStdin": true,
"AttachStdout": true,
"Cmd": [
"/bin/bash"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": null,
"Hostname": "ddb7b55902cc",
"Image": "ubuntu:14.04",
"MacAddress": "",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": true,
"PortSpecs": null,
"StdinOnce": true,
"Tty": true,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Created": "2015-05-08T22:41:44.74862921Z",
"Driver": "devicemapper",
"ExecDriver": "native-0.2",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/tmp:/tmp"
],
"CapAdd": null,
"CapDrop": null,
"ContainerIDFile": "",
"Devices": [],
"Dns": null,
"DnsSearch": null,
"ExtraHosts": null,
"IpcMode": "",
"Links": null,
"LxcConf": [],
"NetworkMode": "bridge",
"PidMode": "",
"PortBindings": {},
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"RestartPolicy": {
"MaximumRetryCount": 0,
"Name": ""
},
"SecurityOpt": null,
"VolumesFrom": null
},
"HostnamePath": "/var/lib/docker/containers/ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a/hostname",
"HostsPath": "/var/lib/docker/containers/ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a/hosts",
"Id": "ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a",
"Image": "ed5a78b7b42bde1e3e4c2996e02da778882dca78f8919cbd0deb6694803edec3",
"MountLabel": "",
"Name": "/distracted_banach",
"NetworkSettings": {
"Bridge": "docker0",
"Gateway": "172.17.42.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "fe80::42:acff:fe11:4",
"LinkLocalIPv6PrefixLen": 64,
"MacAddress": "02:42:ac:11:00:04",
"PortMapping": null,
"Ports": {}
},
"Path": "/bin/bash",
"ProcessLabel": "",
"ResolvConfPath": "/var/lib/docker/containers/ddb7b55902cc328612d794570fe9a936d96a9644411e89c4ea116a5fef4c311a/resolv.conf",
"RestartCount": 0,
"State": {
"Error": "",
"ExitCode": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"OOMKilled": false,
"Paused": false,
"Pid": 6115,
"Restarting": false,
"Running": true,
"StartedAt": "2015-05-08T22:41:45.367432585Z"
},
"Volumes": {
"/tmp": "/tmp"
},
"VolumesRW": {
"/tmp": true
}
}
]
docker history <image name>
will show the layers baked into an image. Unfortunately, docker history
seems hobbled by its formatting and lack of options to choose what is displayed.
docker history 将显示烘焙到图像中的图层。不幸的是,泊坞历史似乎因格式化而缺乏选择显示内容的选项。
You can choose terse and verbose formats, via the --no-trunc flag.
您可以通过--no-trunc标志选择简洁和详细格式。
$ docker history drpaulbrewer/spark-worker
IMAGE CREATED CREATED BY SIZE
438ff4e1753a 2 weeks ago /bin/sh -c #(nop) CMD [/bin/sh -c /spark/my-s 0 B
6b664e299724 2 weeks ago /bin/sh -c #(nop) ADD file:09da603c5f0dca7cc6 296 B
f6ae126ae124 2 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaf 0 B
70bcb3ffaec9 2 weeks ago /bin/sh -c #(nop) EXPOSE 2222/tcp 4040/tcp 60 0 B
1332ac203849 2 weeks ago /bin/sh -c apt-get update && apt-get --yes up 1.481 GB
8e6f1e0bb1b0 2 weeks ago /bin/sh -c sed -e 's/archive.ubuntu.com/www.g 1.975 kB
b3d242776b1f 2 weeks ago /bin/sh -c #(nop) WORKDIR /spark/spark-1.3.1 0 B
ac0d6cc5aa3f 2 weeks ago /bin/sh -c #(nop) ADD file:b6549e3d28e2d149c0 25.89 MB
6ee404a44b3f 5 weeks ago /bin/sh -c #(nop) WORKDIR /spark 0 B
c167faff18cf 5 weeks ago /bin/sh -c adduser --disabled-password --home 335.1 kB
f55d468318a4 5 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaf 0 B
19c8c047d0fe 8 weeks ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B
c44d976a473f 8 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 1.879 kB
14dbf1d35e28 8 weeks ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic 701 B
afa7a164a0d2 8 weeks ago /bin/sh -c #(nop) ADD file:57f97478006b988c0c 131.5 MB
511136ea3c5a 23 months ago 0 B
Here's a verbose example.
这是一个冗长的例子。
docker history --no-trunc=true drpaulbrewer/spark-worker
IMAGE CREATED CREATED BY SIZE
438ff4e1753a60779f389a3de593d41f7d24a61da6e1df76dded74a688febd64 2 weeks ago /bin/sh -c #(nop) CMD [/bin/sh -c /spark/my-spark-worker.sh] 0 B
6b664e29972481b8d6d47f98167f110609d9599f48001c3ca11c22364196c98a 2 weeks ago /bin/sh -c #(nop) ADD file:09da603c5f0dca7cc60f1911caf30c3c70df5e4783f7eb10468e70df66e2109f in /spark/ 296 B
f6ae126ae124ca211c04a1257510930b37ea78425e31a273ea0b1495fa176c57 2 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaftc.com 0 B
70bcb3ffaec97a0d14e93b170ed70cc7d68c3c9dfb0222c1d360a300d6e05255 2 weeks ago /bin/sh -c #(nop) EXPOSE 2222/tcp 4040/tcp 6066/tcp 7077/tcp 7777/tcp 8080/tcp 8081/tcp 0 B
1332ac20384947fe1f15107213b675e5be36a68d72f0e81153d6d5a21acf35af 2 weeks ago /bin/sh -c apt-get update && apt-get --yes upgrade && apt-get --yes install sed nano curl wget openjdk-8-jdk scala && echo "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64" >>/etc/environment && export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m" && ./build/mvn -Phive -Phive-thriftserver -DskipTests clean package && chown -R spark:spark /spark && mkdir /var/run/sshd 1.481 GB
8e6f1e0bb1b0b9286947d3a4b443cc8099b00f9670aab1d58654051e06f62e51 2 weeks ago /bin/sh -c sed -e 's/archive.ubuntu.com/www.gtlib.gatech.edu\/pub/' /etc/apt/sources.list > /tmp/sources.list && mv /tmp/sources.list /etc/apt/sources.list 1.975 kB
b3d242776b1f1f1ae5685471d06a91a68f92845ef6fc6445d831835cd55e5d0b 2 weeks ago /bin/sh -c #(nop) WORKDIR /spark/spark-1.3.1 0 B
ac0d6cc5aa3fdc3b65fc0173f6775af283c3c395c8dae945cf23940435f2785d 2 weeks ago /bin/sh -c #(nop) ADD file:b6549e3d28e2d149c0bc84f69eb0beab16f62780fc4889bcc64cfc9ce9f762d6 in /spark/ 25.89 MB
6ee404a44b3fdd3ef3318dc10f3d002f1995eea238c78f4eeb9733d00bb29404 5 weeks ago /bin/sh -c #(nop) WORKDIR /spark 0 B
c167faff18cfecedef30343ef1cb54aca45f4ef0478a3f6296746683f69d601b 5 weeks ago /bin/sh -c adduser --disabled-password --home /spark spark 335.1 kB
f55d468318a4778733160d377c5d350dc8f593683009699c2af85244471b15a3 5 weeks ago /bin/sh -c #(nop) MAINTAINER drpaulbrewer@eaftc.com 0 B
19c8c047d0fe2de7239120f2b5c1a20bbbcb4d3eb9cbf0efa59ab27ab047377a 8 weeks ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B
c44d976a473f143937ef91449c73f2cabd109b540f6edf54facb9bc2b4fff136 8 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list 1.879 kB
14dbf1d35e2849a00c6c2628055030fa84b4fb55eaadbe0ecad8b82df65cc0db 8 weeks ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/policy-rc.d && echo 'exit 101' >> /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d && dpkg-divert --local --rename --add /sbin/initctl && cp -a /usr/sbin/policy-rc.d /sbin/initctl && sed -i 's/^exit.*/exit 0/' /sbin/initctl && echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup && echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean && echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean && echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean && echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes 701 B
afa7a164a0d215dbf45cd1aadad2a4d12b8e33fc890064568cc2ea6d42ef9b3c 8 weeks ago /bin/sh -c #(nop) ADD file:57f97478006b988c0c68e5bf82684372e427fd45f21cd7baf5d974d2cfb29e65 in / 131.5 MB
511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158 23 months ago 0 B
#2
With docker 1.10, you now have new commands for data-volume containers.
(for regular containers, see the next section, for docker 1.8+):
使用docker 1.10,您现在可以使用新的数据卷容器命令。 (对于常规容器,请参阅下一节,对于docker 1.8+):
docker volume ls
-
docker volume inspect
码头工人量ls
码头工量检查
With docker 1.8.1 (August 2015), a docker inspect -f '{{ .Volumes }}' containerid
would be empty!
使用docker 1.8.1(2015年8月),docker inspect -f'{{.Volumes}}'containerid将为空!
You now need to check Mounts
, which is a list of mounted paths like:
您现在需要检查Mounts,这是一个已安装路径的列表,如:
"Mounts": [
{
"Name": "7ced22ebb63b78823f71cf33f9a7e1915abe4595fcd4f067084f7c4e8cc1afa2",
"Source": "/mnt/sda1/var/lib/docker/volumes/7ced22ebb63b78823f71cf33f9a7e1915abe4595fcd4f067084f7c4e8cc1afa2/_data",
"Destination": "/home/git/repositories",
"Driver": "local",
"Mode": "",
"RW": true
}
],
If you want the path of the first mount (for instance), that would be (using index 0):
如果你想要第一个mount(例如)的路径,那就是(使用索引0):
docker inspect -f '{{ (index .Mounts 0).Source }}' containerid
As Mike Mitterer comments below:
正如Mike Mitterer在下面评论:
Pretty print the whole thing:
漂亮打印整件事:
docker inspect -f '{{ json .Mounts }}' containerid | python -m json.tool
Or, as commented by Mitja, use the jq
command.
或者,如Mitja所评论的那样,使用jq命令。
docker inspect -f '{{ json .Mounts }}' containerid | jq
#3
Show names and mount point destinations of volumes used by a container:
显示容器使用的卷的名称和装入点目标:
docker container inspect \
-f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}' \
CONTAINER_ID_OR_NAME
This is compatible with Docker 1.13.
这与Docker 1.13兼容。
#4
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.
您可以通过检查容器并查看JSON输出并比较几个字段来获取有关哪些卷专门烘焙到容器中的信息。当您运行docker inspect myContainer时,Volumes和VolumesRW字段会为您提供有关容器内安装的所有卷的信息,包括使用VOLUME指令在Dockerfile中安装的卷,以及使用docker run -v命令在命令行上安装的卷。但是,您可以通过检查docker中的HostConfig.Binds字段检查JSON输出,使用docker run -v命令隔离容器中装入的卷。为了澄清,此HostConfig.Binds字段告诉您使用-v选项在docker run命令中专门装入了哪些卷。因此,如果您使用Volumes字段交叉引用此字段,则可以使用Dockerfile中的VOLUME指令确定将哪些卷烘焙到容器中。
A grep could accomplish this like:
grep可以实现这一点:
$ docker inspect myContainer | grep -C2 Binds
...
"HostConfig": {
"Binds": [
"/var/docker/docker-registry/config:/registry"
],
And...
$ docker inspect myContainer | grep -C3 -e "Volumes\":"
...
"Volumes": {
"/data": "/var/lib/docker...",
"/config": "/var/lib/docker...",
"/registry": "/var/docker/docker-registry/config"
And in my example, you can see I've mounted /var/docker/docker-registry/config
into the container as /registry
using the -v
option in my docker run
command, and I've mounted the /data
and /config
volumes using the VOLUME
directive in my Dockerfile. The container does not need to be running to get this information, but it needs to have been run at least one time in order to populate the HostConfig
JSON output of your docker inspect
command.
在我的例子中,您可以看到我已经使用我的docker run命令中的-v选项将/ var / docker / docker-registry / config作为/ registry挂载到容器中,并且我已经挂载了/ data和/ config我的Dockerfile中使用VOLUME指令的卷。容器不需要运行来获取此信息,但它需要至少运行一次才能填充docker inspect命令的HostConfig JSON输出。
#5
For Docker 1.8, I use:
对于Docker 1.8,我使用:
$ docker inspect -f "{{ .Config.Volumes }}" 957d2dd1d4e8
map[/xmount/dvol.01:{}]
$
#6
Here is one line command to get the volume information for running containers:
以下是一行命令,用于获取运行容器的卷信息:
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
Output is:
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[]
Docker version:
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
#7
Useful variation for docker-compose users:
docker-compose用户的有用变体:
docker-compose ps -q | xargs docker container inspect \
-f '{{ range .Mounts }}{{ .Name }}:{{ .Destination }} {{ end }}'
This will very neatly output parseable volume info. Example from my wordpress docker-compose:
这将非常整齐地输出可解析的音量信息。我的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
The output contains one line for each container, listing the volumes (and mount points) used. Alter the {{ .Name }}:{{ .Destination }} portion to output the info you would like.
输出包含每个容器的一行,列出使用的卷(和挂载点)。更改{{.Name}}:{{.Destination}}部分以输出您想要的信息。
If you just want a simple list of volumes, one per line
如果您只想要一个简单的卷列表,每行一个
$ 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.
非常适合生成要备份的卷列表。我使用这种技术和Blacklabelops Volumerize来备份docker-compose中所有容器使用的所有卷。 Volumerize的文档不会调用它,但您不需要在持久容器中使用它,也不需要使用内置工具来启动和停止服务。我更喜欢将关键操作(如备份和服务控制)留给实际用户(docker之外)。我的备份由实际(非docker)用户帐户触发,并使用docker-compose stop来停止服务,备份所有正在使用的卷,最后docker-compose start to restart。
#8
if you want to list all the containers name with the relevant volumes that attached to each container you can try this:
如果要列出所有容器名称以及附加到每个容器的相关卷,可以尝试:
docker ps -q | xargs docker container inspect -f '{{ .Name }} {{ .HostConfig.Binds }}'
example output:
/opt_rundeck_1 [/opt/var/lib/mysql:/var/lib/mysql:rw /var/lib/rundeck/var/storage:/var/lib/rundeck/var/storage:rw /opt/var/rundeck/.ssh:/var/lib/rundeck/.ssh:rw /opt/etc/rundeck:/etc/rundeck:rw /var/log/rundeck:/var/log/rundeck:rw /opt/rundeck-plugins:/opt/rundeck-plugins:rw /opt/var/rundeck:/var/rundeck:rw]
/ opt_rundeck_1 [/ opt / var / lib / mysql:/ var / lib / mysql:rw / var / lib / rundeck / var / storage:/ var / lib / rundeck / var / storage:rw / opt / var / rundeck / .ssh:/var/lib/rundeck/.ssh:rw / opt / etc / rundeck:/ etc / rundeck:rw / var / log / rundeck:/ var / log / rundeck:rw / opt / rundeck-plugins:/ opt / rundeck-plugins:rw / opt / var / rundeck:/ var / rundeck:rw]
/opt_rundeck_1 - container name
/ opt_rundeck_1 - 容器名称
[..] - volumes attached to the conatiner
[..] - 附在容器上的卷
#9
docker inspect -f '{{ json .Mounts }}' containerid | jq '.[]'
#10
Here is my version to find mount points of a docker compose. In use this to backup the volumes.
这是我的版本,用于查找docker compose的挂载点。在使用它来备份卷。
# 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
This is a combination of previous solutions.
这是以前解决方案的组合。