大网数据广告活动
服务器运维问答

Use auto mounted USB drives in Docker container

· 52CE 我爱测

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>:/med…

问题原文

原帖链接 · Mouagip · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · OmerSch · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。