Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list Docker mounted volumes from within the container

Tags:

I want to list all container directories that are mounted volumes.

I.e. to be able to get similar info I get from

docker inspect --format "{{ .Volumes }}" <self>

But from within the container and without having docker installed in there.

I tried cat /proc/mounts, but I couldn't find a proper filter for it.

like image 487
Leo Gallucci Avatar asked Jun 04 '15 11:06

Leo Gallucci


People also ask

How can I see docker volumes?

View a Data Volume You can use the docker volume ls command to view a list of data volumes. Use the docker volume inspect command to view the data volume details.

Where are docker volumes mounted?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).

Which is the docker syntax to locate a volume which is mounted to a container?

Using Docker's “volume create” command The docker volume create command will create a named volume. The name allows you to easily locate and assign Docker volumes to containers.


2 Answers

(EDIT - this may no longer work on Mac) If your Docker host is OS X, the mounted volumes will be type osxfs (or fuse.osxfs). You can run a

mount | grep osxfs | awk '{print $3}'

and get a list of all the mounted volumes.

If your Docker host is Linux (at least Ubuntu 14+, maybe others), the volumes appear to all be on /dev, but not on a device that is in your container's /dev filesystem. The volumes will be alongside /etc/resolv.conf, /etc/hostname, and /etc/hosts. If you do a mount | grep ^/dev to start, then filter out any of the files in ls /dev/*, then filter out the three files listed above, you should be left with host volumes.

mount | grep ^/dev/ | grep -v /etc | awk '{print $3}'

My guess is the specifics may vary from Linux to Linux. Not ideal, but at least possible to figure out.

like image 79
Ryan Calhoun Avatar answered Sep 17 '22 14:09

Ryan Calhoun


Assuming you want to check what volumes are mounted from inside a linux based container you can look up entries beginning with "/dev" in /etc/mtab, removing the /etc entries

$ grep "^/dev" /etc/mtab | grep -v " \/etc/"

/dev/nvme0n1p1 /var/www/site1 ext4 rw,relatime,discard,data=ordered 0 0
/dev/nvme0n1p1 /var/www/site2 ext4 rw,relatime,discard,data=ordered 0 0
like image 41
SRC Avatar answered Sep 17 '22 14:09

SRC