通过动态USB安装到Docker容器

发布于 2025-02-09 12:35:01 字数 1371 浏览 1 评论 0原文

我有一个Docker应用程序,可以通过Nodejs + React应用程序读取任何USB的图像。

我使用UDEV规则,SystemD和Bash脚本在Linux框上本地工作:

# Call when a device is inserted
KERNEL=="sd[a-z]*", \
ACTION=="add", \
RUN+="/usr/bin/systemctl --no-block restart automount@%k.service"

# Call when a device is removed
KERNEL=="sd[a-z]*", \
ACTION=="remove", \
RUN+="/usr/bin/systemctl --no-block restart automount@%k.service"
[Service]
Type=forking
GuessMainPID=no
ExecStart=/usr/bin/bash /some/dir/scripts/example.sh %I

BASH脚本将采用Dev Path(即,/dev/dev/sdc1),并使用pmount < /code>在/Media/USB -Mount下安装设备:/usr/bin/bin/pmount -u 000 -t“ $ type” $ dev_path“ usb -mount

插入USB设备时,它在/Media/USB-Mount下正确安装。但是,我不知道如何将此目录传递到Docker容器。我已将以下规则添加到我的docker Run命令:

--device-cgroup-rule="c 8:* rmw" \
-v /media/:/host_media/ \

第一个规则来自此处,第二条规则安装了/Media主机>主机>主机目录到// host_media容器目录。

这部分起作用。我能够将USB设备与系统插入并断开连接,并且看到在容器中创建的目录。但是,上的文件缺少USB驱动器。

我如何也可以在驱动器上看到所有内容?还是有更好的方法可以做到这一点?

I have a Docker application that will be able to read images off of any USB via a NodeJS + React application.

I have this working natively on my Linux box using udev rules, systemd, and a bash script:

# Call when a device is inserted
KERNEL=="sd[a-z]*", \
ACTION=="add", \
RUN+="/usr/bin/systemctl --no-block restart automount@%k.service"

# Call when a device is removed
KERNEL=="sd[a-z]*", \
ACTION=="remove", \
RUN+="/usr/bin/systemctl --no-block restart automount@%k.service"
[Service]
Type=forking
GuessMainPID=no
ExecStart=/usr/bin/bash /some/dir/scripts/example.sh %I

The bash script will take the dev path (i.e., /dev/sdc1) and use pmount to mount the device under /media/usb-mount: /usr/bin/pmount -u 000 -t "$TYPE" "$DEV_PATH" usb-mount.

When plugging in a USB device, it correctly mounts under /media/usb-mount. However, I cannot figure out how to pass this directory through to a docker container. I have added the following rules to my docker run command:

--device-cgroup-rule="c 8:* rmw" \
-v /media/:/host_media/ \

The first rule comes from here and the second rule mounts the /media host directory to the /host_media container directory.

This partially works. I am able to plug in and disconnect USB devices from my system, and I see the directories being created inside the container. However, the files on the USB drive are missing.

How can I see everything on the drive as well? Or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2025-02-16 12:35:01

我遇到了完全相同的问题,我终于设法找到了在这里起作用的答案: https://serverfault.com/questions/903534/use-auto-mounted-usb-drives-in-docker-container

要点是,这些文件是在Docker容器中正确复制的,但是安装了安装不是。使用绑定安装固定式粘合式传播设置与(r)私有之外的其他东西,使容器可以正确看到安装座。就我而言,看起来基本上是相同的,(r)奴隶做到了。

请参阅 https://docs.docks.docker.com/storage/storage/bind--bind--安装/#配置绑定范围

I had the exact same problem and I finally managed to find an answer that worked here: https://serverfault.com/questions/903534/use-auto-mounted-usb-drives-in-docker-container

The gist is that the files are correctly replicated in the docker container but the mounting itself isn't. Using a bind mount with bind propagation set to something other than (r)private lets the container see the mounts correctly. In my case, which looks basically identical, (r)slave did the trick.

See https://docs.docker.com/storage/bind-mounts/#configure-bind-propagation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文