【问题标题】:docker check for changes on mounted file systemdocker 检查挂载文件系统上的更改
【发布时间】:2018-12-18 07:17:13
【问题描述】:

我想在 docker 容器中创建一个 ruby​​ 构建环境。 Ruby 安装在容器中,我可以在我的 win10 主机上进行开发。我将我的主机工作目录安装到容器并构建项目。但现在我想到了一点生活质量的改善:如果容器只监听挂载的文件系统,并在我更改项目中的某些内容时进行构建,那不是很好吗?

在谷歌 5 分钟后,我发现 inotifywait 乍一看看起来很棒,所以我首先尝试没有安装文件系统:

[root@a3193720c4f1 testfolder]# inotifywait -m /tmp/testfolder/ -e modify -e create -e open |
>     while read path action file; do
>         echo "The file '$file' appeared in directory '$path' via '$action'"
>     done
Setting up watches.
Watches established.
The file '' appeared in directory '/tmp/testfolder/' via 'OPEN,ISDIR'
The file 'newfile' appeared in directory '/tmp/testfolder/' via 'OPEN'

这成功了,但/tmp/testfolder 不是挂载目录,它只是我在容器中创建的一个测试目录,用于习惯 inotifywait 命令。所以这次我启动了我的容器并挂载了目录:

docker run -it --rm -v "D:\LokaleDaten\ELK_Docker\Docker\RVM\testfolder":"/tmp/testfolder" -w "/tmp/testfolder" myrvm

然后我再次启动 inotifywait,更改了一个文件(在安装到容器的主机系统上),但 inotify 没有捕获更改的文件。我还检查了容器中的文件是否已更改,是的,已进行了更改。 所以我很好奇我是否遗漏了什么,或者是 inotify 不是合适的工具?

亲切的问候,曼努埃尔

【问题讨论】:

  • 我使用过类似的here 并且效果很好。 inotifywait 应该可以工作,但可能是 Windows 文件系统对文件更改的行为与 linux 不同...
  • 好吧,我想我可能已经找到了问题blog.subjectify.us/miscellaneous/2017/04/24/… 但是在我的用例中使用 python 脚本不起作用。
  • 我宁愿有一个工具在我的容器中持续轮询文件系统,而不是在我的主机上。

标签: docker inotify


【解决方案1】:

由于列出的解决方法here 需要您安装 python,所以我一直在寻找。然后我找到了一个solution,您不必安装python,但需要在我的主机上安装不同的工具。

所以基本上我最终得到的是一个自制的脚本,它每隔几秒就轮询一次文件系统并检查目录的 md5sum 是否有变化。如果有人需要,这里是脚本(注意:这是我制作的第一个 bash 脚本,所以如果有什么不工作或不好的做法,请随时发表评论!):

#!/bin/sh
# script that gets a hash of a directory and checks every few seconds if the hash has changed
# if it has changed, it executes a command

if [ -d $1 ] || [ -f $1 ]; then
 f="$1"
else
 echo $(date +"%d.%m.%Y %T %:::z") ERROR: arg1 is neither a directory nor a file
 exit 1
fi

shift

if [ -z "$*" ]; then
 echo $(date +"%d.%m.%Y %T %:::z") ERROR: arg2 is required otherwise no command is executed
 exit 1
fi
cmd=$*

readonly getmd5sum="tar -cP $f | md5sum"
md5val="`eval ${getmd5sum}`"

while : ; do
 if [[ $md5val != `eval ${getmd5sum}` ]]; then
  md5val="`eval ${getmd5sum}`"
  $cmd
 fi
 sleep 2
done

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2019-10-08
    • 2017-12-20
    • 1970-01-01
    相关资源
    最近更新 更多