6

I would like to mount and unmount USB drives such that their contents can be seen from within a Docker container while it is running. I've mapped the directory to the container which the USB drives will be mounted to.

The problem:

  • The docker container cannot see folders/files that are mounted to its volume mapped folder if the container was started before mounting the USB drives.
  • If the container is started after the USB is mounted the files can be seen, however, unmounting and mounting again will not show the contents.


Here's what I've tried:

I will be mounting a USB to the directory /some_folder/usb which is mapped to a docker container on Ubuntu 18.04 LTS w/ 4.15 kernel.

Start a container with the volume /some_folder mapped to the same named directory inside it:

$ docker run --name mycontainer -it -v /some_folder:/some_folder ubuntu bash

Detach from the container (CTRL+P+Q), connect a USB drive and mount it to the host directory as root:

$ mount /dev/sda1 /some_folder/usb/

Mount options:

$ mount | grep -i sda1
/dev/sda1 on /some_folder/usb type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

Verify there are files mounted to the host directory:

$ ls -al /some_folder/usb/
drwxr-xr-x 10 root root 8192 Jan  1  1970  .
drwxr-xr-x  3 root root 1024 Mar 13 16:44  ..
drwxr-xr-x  5 root root 8192 Jul 20  2018  files

Enter the container and check the mapped directory:

$ docker exec -it mycontainer bash
root@mycontainer# ls -al /some_folder/usb

Contents of mounted folder is empty inside container.

2
  • Have you already tried setting mount options that override default and allow wider access already? Can you show the output of mount | grep -i sda1 when the usb is mounted, and you can see the files in the terminal, but not when checking from docker.
    – NetIceGear
    Mar 16, 2019 at 1:43
  • @BarBar1234 I haven't tried any other mount options other than read-only. Do you have some options I can try? I've update my answer to show the output of mount | grep -i sda1 on the host.
    – Ryan R
    Mar 18, 2019 at 17:23

1 Answer 1

0

I know the question is old, but a solution would be using bind-mounts and setting the "bind propagation" to "shared".

Example:

docker run --name mycontainer -it --mount type=bind,source=/some_folder,target=/some_folder,bind-propagation=shared ubuntu bash

More info about bind-propagation: https://docs.docker.com/storage/bind-mounts/#configure-bind-propagation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .