How to list the content of a named volume in docker 1.9+?
DockerDocker Problem Overview
Docker 1.9 added named volumes, so I..
docker volume create --name postgres-data
docker volume ls
and I get
local postgres-data
all good so far..
so how do I see what is in the named volume? Is there a way to cd to it on the host system. Like I can for a mounted host directory?
Docker Solutions
Solution 1 - Docker
docker run --rm -i -v=postgres-data:/tmp/myvolume busybox find /tmp/myvolume
Explanation: Create a minimal container with tools to see the volume's files (busybox), mount the named volume on a container's directory (v=postgres-data:/tmp/myvolume
), list the volume's files (find /tmp/myvolume
). Remove the container when the listing is done (--rm
).
Solution 2 - Docker
you can run
docker volume inspect postgres-data
and see Mountpoint
section of the result
therefore source parameter will point to host directory
maybe
/var/lib/docker/volumes/[volume_name]/_data
directory
Solution 3 - Docker
You can see where docker is storing a volume by running docker volume inspect <volume>
.
But there's a caveat: You can't directly see the contents of volumes on Mac and Windows. This occurs because Docker actually runs a Linux VM to be able to containerize, since containzerzation is native functionality for Linux but not these others OSes. So the path that appears is actually the path inside the VM, and not on your host system.
You can access these volumes by using the methods mentioned in the other answers (create a ephemeral container just to view the content) or you can access these directly.
For Mac
For Mac, you can use screen
to get access to the VM, like so:
# This path can be slightly different on your system
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
And once there, you can navigate to the path that docker volume inspect
gave you.
For Windows
With Docker Desktop & WSL2
Navigate to \\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes
in Windows Explorer
Solution 4 - Docker
Here's one idea...
docker run -it --name admin -v postgres-data:/var/lib/postgresql/data ubuntu
then in the interactive shell
ls /var/lib/postgresql/data
Better ideas welcome!
Solution 5 - Docker
Or no need for jq or the new container.
cd
to the directory:
cd $(docker volume inspect <volume name> | grep Mountpoint | cut -d\" -f 4)
View the content of the directory:
ls -las $(docker volume inspect <volume name> | grep Mountpoint | cut -d\" -f 4)
Even better! View the content of all volumes:__
for i in `docker volume ls -q`; do echo volume: ${i}; \
ls -las $(docker volume inspect $i | grep Mountpoint | cut -d\" -f 4); \
done
Using it all the time, when need to find something quickly.
Solution 6 - Docker
With *jq
:
$ sudo ls -l $(docker volume inspect VOLUME_NAME | jq -r '.[0].Mountpoint')
*Check jq doc page for details on how to install it if not yet (PACKAGE_MANAGER install jq
).
Solution 7 - Docker
I use this handy function to list the content of my volumes:
dvolume() {
local volume volumes_to_list=${1:-$(docker volume ls --quiet)}
for volume in $volumes_to_list; do
sudo ls -lRa "$(docker volume inspect --format '{{ .Mountpoint }}' "$volume")"
echo
done
}
Notice you can call the function in two ways:
$ dvolume # for each volume, list its content
$ dvolume <volume> # list <volume>'s content