adelton

Inspecting Docker containers for volume information

Jan Pazdziora

2015-09-14


Abstract

Docker 1.8 seems to have dropped Volumes (and VolumesRW) from the docker inspect output. New --format is thus needed to get information about volume mountpoints.

Docker 1.7 inspect output

Up to Docker 1.7, docker inspect provided information about volume mounts in Volumes.

Let's assume we've bind mounted directory /tmp/test to a container:

host$ mkdir /tmp/test
host$ echo test-mount > /tmp/test/test
host$ docker run --name test-mount -ti -v /tmp/test:/data:Z fedora:22 cat /data/test
test-mount

We could then get information about the directory being mounted with docker inspect and would see

host$ docker inspect test-mount
[
{
...
    "Volumes": {
        "/data": "/tmp/test"
    },
    "VolumesRW": {
        "/data": true
    },
...
}
]

If we only wanted to output the name of directory being mounted as /data, we would have run

host$ docker inspect --format '{{ index .Volumes "/data" }}' test-mount
/tmp/test

The use of index here was needed because the key /data is not alpha numeric so we couldn't have done '{{ .Volumes./data }}'

reflect: call of reflect.Value.Type on zero Value

With Docker 1.8, the Volumes structure is missing from the docker inspect output. When we run inspect with the original format, we get an error:

host$ docker inspect --format '{{ index .Volumes "/data" }}' test-mount
reflect: call of reflect.Value.Type on zero Value

New structure in Docker 1.8

When we run the same docker run -v /tmp/test:/data:Z with Docker 1.8 and then inspect the container, we will see the format has changed — Volumes are gone and there are Mounts instead, with different structure:

host$ docker inspect test-mount
[
{
...
    "Mounts": [
        {
            "Source": "/tmp/test",
            "Destination": "/data",
            "Mode": "Z",
            "RW": true
        }
    ],
...
}
]

To output the same information as with older versions, the directory being mounted into /data, the format needs to be changed a bit:

host$ docker inspect --format '{{ range .Mounts }}{{ if eq .Destination "/data" }}{{ .Source }}{{ end }}{{ end }}' test-mount
/tmp/test

Since the structure is no longer a map but array, we need to do a little bit more work to cycle through the list with range to find the mount point we are interested in.