如何在 docker 1.9 + 中列出命名卷的内容?

Docker 1.9添加了命名卷,所以我..。

docker volume create --name postgres-data


docker volume ls

我得到了

local               postgres-data

目前一切正常。

那么如何查看命名卷中的内容呢?有没有一种方法可以在主机系统上对它进行 cd 操作。就像挂载的主机目录一样?

75454 次浏览

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!

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

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).

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

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).

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.

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