How do you list volumes in docker containers?

2019-01-04 06:41发布

When using docker images from registries, I often need to see the volumes created by the image's containers.

Note: I'm using docker version 1.3.2 on Red Hat 7.

Example

The postgres official image from the Docker Registry has a volume configured for containers at /var/lib/postgresql/data.

What's the most succinct command to show the volume at /var/lib/postgresql/data in a postgres container?

标签: docker
10条回答
该账号已被封号
2楼-- · 2019-01-04 07:28

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
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-04 07:33

Useful variation for docker-compose users:

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:

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.

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-04 07:38

Here is my version to find mount points of a docker compose. In use this to backup the volumes.

 # 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.

查看更多
聊天终结者
5楼-- · 2019-01-04 07:40

For Docker 1.8, I use:

$ docker inspect -f "{{ .Config.Volumes }}" 957d2dd1d4e8
map[/xmount/dvol.01:{}]
$ 
查看更多
登录 后发表回答