5

I have been searching for a few hours and can't seem to find the most current answer to this problem

Problem: Apparently VirtualBox is being used by Docker on MacOS and it does not want to pass file notifications from the Host OS to the container. As a result, inotifywait will only notify file activity on a watched folder when those actions are performed from within the container. NOT when those actions are performed from the Host.

Dockerfile

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

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

build the container

docker build -f Dockerfile -t my_container .

run the container

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

the log.txt file is created and has lines for any actions in the watchfolder that were initiated from within the container shell. But NOT if the action is performed from the Host on this mounted volume

I am using a python base image because I have a python script that I want to run on any new file that comes into that watchfolder or its subdirectories. This docker container will run on my debian server, but I'm developing on a mac.

OSUDamian
  • 161
  • 1
  • 12
  • Can you set up an HTTP listener (maybe using Flask) and send files to the process using HTTP POST requests, instead of relying on filesystem events? – David Maze Nov 25 '21 at 23:16
  • @David Maze. Thank you for the suggestion. I'm not sure if that would work since I haven't set anything like that up. But just based on your explanation, I wonder if that would work for me, since these files will not be coming in via POST, they will be moved into the watchfolder manually – OSUDamian Nov 26 '21 at 04:09
  • Excellently written question. Thank you for providing such detailed troubleshooting steps. I suspect the question will be as useful to other people as the answer, if not more. I made two small changes in my tests: 1. Move the apt-get commands at the beginning of the Dockerfile so that modifying main.sh doesn’t re-run them when rebuilding; 2. Replace `>> log.txt` with `| tee -a log.txt` so that the actions also appear in the terminal when running the container. – Olivier 'Ölbaum' Scherler Jun 07 '22 at 21:19

1 Answers1

4

After 2 days of fiddling I came across this solution.

In docker for mac, go to Preferences > General > Use gRPC FUSE for file sharing [TURN THIS FEATURE OFF - it is on by default].

In addition to checking this off, I also was careful to use the correct capitalization on the volume mount (for example -v user/data:/data was not correct, should be -v User/data:/data)

According to docker documentation, 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.

So after those two changes, I was able to get inotifywait to log changes made to the watchfolder in the mount even when the event was performed from the Host OS.

OSUDamian
  • 161
  • 1
  • 12
  • I has the same problem, but switching off gRPC fuse did not work for me, even worse, on M1 this just prevented docker from starting. – Alleo Mar 24 '22 at 02:21