FileSystemWatcher inside Docker does not notice changes to local directory

Viewed 1599

My program inside a docker container uses FileSystemWatcher to monitor a local folder. I mounted the directory with:

docker run -v /c/Users/Support/Desktop/inbox:/Users/Support/Desktop/inbox -v /c/Users/Support/Desktop/outbox:/Users/Support/Desktop/outbox -it --name workbeanRun workbean

I used Docker Exec to look into the container while it was running. It can see the inbox and outbox directories along with any files that are in it. However, when I throw a new file into the inbox, the FileSystemWatcher event does not fire. There's nothing wrong with the code because if I don't use a docker container, it runs fine.

Is there anything else I need to do in mounting the directores? Or is FileSystemWatcher even possible inside a container?

Okay, as requested, here's the program:

using System;
using System.IO;

namespace workbean
{
    class Program
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        static string sourceDir = "/Users/Support/Desktop/inbox";
        static string destDir = "/Users/Support/Desktop/outbox";


        static void Main(string[] args)
        {
            Console.WriteLine("Testing...");

            Program p = new Program();

            while (true) { }
        }


        public Program()
        {
            watcher.Path = sourceDir;
            watcher.Filter = "*.*";
            watcher.Created += new FileSystemEventHandler(OnCreated);
            watcher.EnableRaisingEvents = true;

        }


        static void OnCreated(object source, FileSystemEventArgs e)
        {

            string[] files = Directory.GetFiles(sourceDir);
            foreach (var item in files)
            {
                string destDir2 = destDir + "/" + Path.GetFileName(item);
                File.Move(item, destDir2);
            }

        }


    }
}
1 Answers

It appears that this is what is happening:

The inbox folder on my local drive is tied to the inbox folder in the container. If you add files to one, they show up in the other.

However, the FileSystemWatcher is only monitoring the inbox in the container. If I add a file to the inbox in the container, then FileSystemWatcher fires an event.

But when adding a file to the local inbox, nothing happens, because FileSystemWatcher is not monitoring that one, even though the two folders are tied to each other.

Related