Salvatore D'angelo
Salvatore D'angelo

Reputation: 1149

How volume - device mapping works on Docker

I created two Docker volumes:

docker volume create volume1
docker volume create volume2

then I created a Docker container with the command:

docker create -it ... some options ... -v volume1:/var/vcap/store -v volume2:/var/vcap/store2 --privileged myimage /bin/bash

The Docker container has ssh correctly configured so when I ssh in it I see correctly the volume mounted on /var/vcap/store. In particular, I see this:

/dev/sda1 on /var/vcap/store type ext4 (rw,relatime,data=ordered)
/dev/sda1 on /var/vcap/store2 type ext4 (rw,relatime,data=ordered)

In addition, I see also other filesystems on device /dev/sda1:

/dev/sda1 on /etc/resolv.conf type ext4 (rw,relatime,data=ordered)
/dev/sda1 on /etc/hostname type ext4 (rw,relatime,data=ordered)
/dev/sda1 on /etc/hosts type ext4 (rw,relatime,data=ordered)

this confuses me a lot because it's not clear to me how it is possible to have the same device mounted on different folders and be different filesystems. In fact, I verified that if I create a file in /var/vcap/store on the Linux VM that host the container on Mac I see under the folder:

/var/lib/docker/volumes/volume1/_data 

the created file. If I create it under /var/vcap/store2 on Linux VM I see it created under

/var/lib/docker/volumes/volume2/_data

so far so good. My problem is that I created the container as privileged because I want to mount and unmount these filesystems. Unmount works fine but if I try to mount the volume again I do not know which device to use. If I use the command:

mount /dev/sda1 /var/vcap/store

I see under this folder the following content:

cni  containerd  docker  kubeadm  kubelet-plugins  log  lost+found  nfs  swap

that I do not know where it comes from.

The questions are:

  1. When I mount a volume on Docker how it is mapped on /dev/sda1 device?
  2. How is it possible the same device for different filesystems?
  3. Suppose I use Docker in a privileged mode which mount command should I use to mount again the filesystems previously unmounted?

Upvotes: 1

Views: 1609

Answers (1)

Alejandro Galera
Alejandro Galera

Reputation: 3691

Answering your questions:

  1. When I mount a volume on Docker how it is mapped on /dev/sda1 device?

When you create a docker volume, docker creates an internal symlink to /var/lib/docker/volumes/volume_name. If /var/lib/docker/volumes is mounted in /dev/sda1, every volume and mounted file or dir which belongs to /dev/sda1 will appear mounted at the same point because actually they're symlinks.

  1. How is it possible the same device for different filesystems?

It's also answered with no.1 answer: It's the same filesystem, but different internal symlinks. Docker does "virtual" mount, so, you have only one real /dev/sda1 although it appears several. That's why you cannot unmount /dev/sda1 from container. Just one "virtual" docker unmount.

  1. Suppose I use Docker in a privileged mode which mount command should I use to mount again the filesystems previously unmounted?

Virtual mounts which are done by docker run or docker-compose up are not possible to be done inside container, because volumes will be referred to host mounting point (/dev/sda1). Definitively, I recommend to have control of mounting and unmounting from host which launches containers. In case you need mount and unmount from a container, please, mount and unmount different mounting points (for example a samba dir, nfs storage and so) than current /dev/sda1 to avoid misunderstandings.

Upvotes: 1

Related Questions