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?
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 volume ls
docker volume inspect
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:If you want the path of the first mount (for instance), that would be (using index 0):
As Mike Mitterer comments below:
Or, as commented by Mitja, use the
jq
command.Show names and mount point destinations of volumes used by a container:
This is compatible with Docker 1.13.
Use
docker ps
to get the container id.Then
docker inspect -f '{{ .Mounts }}' containerid
Example:
terminal 1
terminal 2
The output
is, apparently, due to the use of the Go language to implement the docker command tools.
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.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.You can choose terse and verbose formats, via the --no-trunc flag.
Here's a verbose example.
if you want to list all the containers name with the relevant volumes that attached to each container you can try this:
example output:
/opt_rundeck_1 - container name
[..] - volumes attached to the conatiner
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
, theVolumes
andVolumesRW
fields give you information about ALL of the volumes mounted inside a container, including volumes mounted in both the Dockerfile with theVOLUME
directive, and on the command line with thedocker run -v
command. However, you can isolate which volumes were mounted in the container using thedocker run -v
command by checking for theHostConfig.Binds
field in thedocker inspect
JSON output. To clarify, thisHostConfig.Binds
field tells you which volumes were mounted specifically in yourdocker run
command with the-v
option. So if you cross-reference this field with theVolumes
field, you will be able to determine which volumes were baked into the container usingVOLUME
directives in the Dockerfile.A grep could accomplish this like:
And...
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 mydocker run
command, and I've mounted the/data
and/config
volumes using theVOLUME
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 theHostConfig
JSON output of yourdocker inspect
command.