Ubuntu – Use auto mounted USB drives in Docker container

dockermountUbuntuusb

With Ubuntu, USB drives are automatically mounted at /media/<user>/<drive> when they are plugged in. I wanted to use this to make newly connected USB drives available in a running Docker container. So I used -v /media/<user>:/media/<user> to mount the whole parent directory of the USB mount points to the container.

This works well if the USB drive was connected before the container starts. In this case the USB mount point has permissions 0777 and belongs to <user>. If a USB drive is connected when the container is already running, the mount point appears in the container but has permissions 0700 and is owned by root instead!

Can anyone shed some light on what is possibly happening here? I can imagine that the USB drives are automatically mounted as 0700 belonging to root by default and afterwards some other mechanism kicks in to change this to 0777 and <user>. Only the change is not picked up by Docker. Is this true?

I already digged through tons of forum threads and mailing lists but couldn't figure out the mechanism. I also tried some udev rules (like this one) but these had no effect.

Best Answer

I know the question is from 2018, but a solution would be using bind-mounts and setting the "bind propagation" to "shared" (not available for volumes).

Instead of:

-v /media/<user>:/media/<user>

Use:

--mount type=bind,source=/media/<user>,target=/media/<user>,bind-propagation=shared

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