简体   繁体   English

使用卷挂载的Docker上的事件监听器不起作用

[英]Event listener on Docker using volume mount not working

Some folder is mounted using: 使用以下方式挂载某些文件夹:

docker run -it  C:\Users\User\Documents\project\input:/app/input/ project:latest 

In the container a Python a watchdog.observers instance is running to detect if new files are added to the host folder. 在Python容器中,一个watchdog.observers实例正在运行,以检测是否有新文件添加到主机文件夹中。 These files end up in the Docker container via the volume mount. 这些文件通过卷挂载最终存储在Docker容器中。 The file created event trigger does not reach the Docker container. 文件创建的事件触发器未到达Docker容器。 I think this is strange because in the Docker container events are also happening right? 我认为这很奇怪,因为在Docker容器中事件也正在发生吗?

When I run my code locally and not in Docker the eventlistener it is working. 当我在本地而不是在Docker中运行代码时,eventlistener正在运行。

Does event listening work in my context? 事件侦听在我的上下文中有用吗?

main.py main.py

from watch import ImagesWatcher

if __name__ == '__main__':
    src_path = "/app/input/"
    ImagesWatcher(src_path).run()

watch.py watch.py

import sys
import time

from watchdog.observers import Observer
from ImagesEventHandler import ImagesEventHandler


class ImagesWatcher:
    def __init__(self, src_path):
        self.__src_path = src_path
        self.__event_handler = ImagesEventHandler()
        self.__event_observer = Observer()


    def run(self):
        self.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            self.stop()

    def start(self):
        self.__schedule()
        self.__event_observer.start()

    def stop(self):
        self.__event_observer.stop()
        self.__event_observer.join()

    def __schedule(self):
        self.__event_observer.schedule(
            self.__event_handler,
            self.__src_path,
            recursive=True
        )

if __name__ == "__main__":
    src_path = sys.argv[1] if len(sys.argv) > 1 else '.'
    ImagesWatcher(src_path).run()

imagesEventHandler.py imagesEventHandler.py

from watchdog.events import RegexMatchingEventHandler

class ImagesEventHandler(RegexMatchingEventHandler):
    THUMBNAIL_SIZE = (128, 128)
    IMAGES_REGEX = [r".*[^_thumbnail]\.jpg$"]

    def __init__(self):
        # self.Analyzer = Analyzer()
        super().__init__(self.IMAGES_REGEX)

    def on_created(self, event):
        self.process(event)

    def process(self, event):
        print("DETECTED")

Dockerfile: Dockerfile:

FROM python:3.6
RUN pip install watchdog
ADD . .
WORKDIR /app/
RUN main.py

一位同事向我推荐了以下内容: https : //github.com/merofeev/docker-windows-volume-watcher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Docker 中为 windows 安装卷? - How to mount a volume in Docker for windows? 如何为我的 docker 安装本地卷? - how to mount a local volume for my docker? 如何将 docker 卷挂载作为 python 子进程运行? - How to run a docker volume mount as a python subprocess? 如何从使用 KubernetesPodOperator 触发它的 Airflow 主机将卷挂载到运行 docker 容器的 Kubernetes Pod - How to mount a volume to a Kubernetes Pod running a docker container from the Airflow host that triggers it using the KubernetesPodOperator 使用boto3挂载EBS卷 - python - Mount EBS volume using boto3 Azure kube.netes - 将 Azure blob 安装为一个卷并使用 python 写入? - Azure kubernetes - mount Azure blob as a volume and write using python? 在 Docker (Ubuntu 20.04) 中使用绑定挂载 - Using bind mount in Docker (Ubuntu 20.04) 使用Docker安装卷 - 没有添加文件 - Mounting Volume Using Docker - No Files Being Added MacOS:如何使用 docker-compose.yml 和 Dockerfile 挂载卷? - MacOS: How to mount volumes using docker-compose.yml and Dockerfile? 使用异步的 python 子进程的“关闭”事件侦听器 - 'close' event listener for python subprocess using asyncio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM