How to list the content of a named volume in docker 1.9+?

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?


Solution 1:

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:

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:

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:

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:

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.