[docker] How do you list volumes in docker containers?

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?

This question is related to docker

The answer is


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.


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.


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

If you are using pwsh (powershell core), you can try

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

also you can see both container name and Mounts as below

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

As the output is converted as json ,you can get any properties it has.


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 - container name

[..] - volumes attached to the conatiner


For Docker 1.8, I use:

$ docker inspect -f "{{ .Config.Volumes }}" 957d2dd1d4e8
map[/xmount/dvol.01:{}]
$ 

I actually googled this, and found my own answer :) My memory these days... And for those that dont know about it commandlinefu is a nice place to find and publish these kinda snippets.

List docker volumes by container.

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" }}' {}

Example output.

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

With docker 1.10, you now have new commands for data-volume containers.
(for regular containers, see the next section, for docker 1.8+):


With docker 1.8.1 (August 2015), a docker inspect -f '{{ .Volumes }}' containerid would be empty!

You now need to check Mounts, which is a list of mounted paths like:

   "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):

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

As Mike Mitterer comments below:

Pretty print the whole thing:

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

Or, as commented by Mitja, use the jq command.

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

From Docker Documentation here

.Mounts Names of the volumes mounted in this container.

docker ps -a --no-trunc --format "{{.ID}}\t{{.Names}}\t{{.Mounts}}"

should work


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.


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.

A grep could accomplish this like:

$ 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 inspect -f '{{ json .Mounts }}' containerid | jq '.[]'

We can do it without the -f Go template syntax:

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

The first jq operation jq .[] strips the object {} wrapper.

The second jq operation will return all the Mount items.