docker - 在 docker 挂载的卷上进行 inotify (Mac/Linux)

标签 docker inotify docker-for-mac inotifywait

我已经搜索了几个小时,似乎找不到这个问题的最新答案

问题:显然 VirtualBox 被 MacOS 上的 Docker 使用,它不想将文件通知从主机操作系统传递到容器。因此,inotifywait 只会在从容器内执行这些操作时通知监视文件夹上的文件事件。当这些操作是从主机执行时不是。

docker 文件

FROM python:3.9.6
COPY main.sh /bin/main.sh
RUN chmod +x /bin/main.sh
RUN apt-get update -y
RUN apt-get install -y inotify-tools
CMD /bin/main.sh

主程序

#!/usr/bin/env bash

inotifywait -mq -r -e create -e modify -e delete -e move /data |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'" >> log.txt
    done

构建容器

docker build -f Dockerfile -t my_container .

运行容器

docker run -it -v /someHOSTdirectory/data:/data my_container my_container

log.txt 文件已创建,其中包含从容器 shell 中启动的 watchfolder 中的任何操作的行。但如果操作是从主机在此安装的卷上执行的,则不是

我正在使用 python 基础镜像,因为我有一个 python 脚本,我想在进入该监视文件夹或其子目录的任何新文件上运行该脚本。这个 docker 容器将在我的 debian 服务器上运行,但我正在 mac 上开发。

最佳答案

经过 2 天的摆弄,我找到了这个解决方案。

在 docker for mac 中,转到首选项 > 常规 > 使用 gRPC FUSE 进行文件共享 [关闭此功能 - 默认情况下打开]。

除了检查这个之外,我还小心地在卷挂载上使用了正确的大写字母(例如 -v user/data:/data 不正确,应该是 -v User/data:/data)

根据 docker 文档,osxfs events are reported

Most inotify events are supported in bind mounts... This means that file >system events from macOS are sent into containers and trigger any listening >processes there.

因此,在这两项更改之后,即使事件是从主机操作系统执行的,我也能够让 inotifywait 记录对挂载中的监视文件夹所做的更改。

关于docker - 在 docker 挂载的卷上进行 inotify (Mac/Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70117645/

相关文章:

docker - 如何修复可在Ubuntu 16.04上运行的docker镜像?

php - 无需轮询即可检测 PHP 中的文件更改

docker - 我的 Docker 服务的 URL 是什么?

macos - 您如何绕过 Docker for Mac 中 Docker.qcow2 的大小限制?

docker - 覆盖Docker守护程序的默认umask

c# - .net核心无法作为独立的应用程序运行。在 docker 上

windows-subsystem-for-linux - 如何在 WSL2 中开启 inotify?

linux - 为什么需要用 inotify_add_watch() 调用 read() 两次

macos - Docker nginx 容器 : http code 400

postgresql - Adminer & Migrate 无法连接到 Postgresql Docker 容器