fsnotify icon indicating copy to clipboard operation
fsnotify copied to clipboard

Edit notifications don't work when running inside of Docker container on Windows

Open matt-github-acct opened this issue 5 years ago • 9 comments

I'm running Windows 10 Enterprise, 10.0.17134

My goal is to launch a docker container, mount a volume inside the container, edit files in the volume from my host operating system and have the docker container recognize the change events. Pretty much this: https://www.zachjohnsondev.com/posts/go-docker-hot-reload-example/

Steps to reproduce

  1. Create main.go file and add the following:
package main

import (
	"log"

	"github.com/fsnotify/fsnotify"
)

func main() {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event, ok := <-watcher.Events:
				if !ok {
					return
				}
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err, ok := <-watcher.Errors:
				if !ok {
					return
				}
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add(".")
	if err != nil {
		log.Fatal(err)
	}
	<-done
}

  1. Create a docker-compose file and add the following:
version: "3.6"

services:
  bar:
    container_name: bar
    build: .
    ports:
      - "8080:8080"
    volumes:
      - ./:/go/src/bar
  1. Create a Dockerfile file and add the following:
FROM golang:latest

WORKDIR /go/src/bar

COPY . .

RUN ["go", "get", "github.com/fsnotify/fsnotify"]

ENTRYPOINT go run main.go
  1. Run docker-compose build in the directory where you've created the files.
  2. Run docker-compose up in the directory where you've created the files.
  3. From your host operating system, add a comment to main.go and notice that no edit events are reported.
  4. Run docker exec -it bar bash to connect to the docker container. Edit main.go from the container shell to confirm that edit events are being reported when the edit is performed within the container.

matt-github-acct avatar Apr 28 '19 23:04 matt-github-acct

Same problem, any solutions?

oxycoder avatar Aug 17 '19 13:08 oxycoder

I'm not sure how Docker for Windows is mounting the shared folder, but generally file shares such as NFS don't support notifications.

A polling fallback (#9) is the only solution I know of when the underlying system doesn't provide events.

For now we should document the limitation, but first we should confirm on other operating systems. It may work on Linux, depending on how the Docker container is mounted there.

nathany avatar Oct 05 '19 20:10 nathany

Currently it work perfectly fine with folder mount to docker on MacOS.

oxycoder avatar Oct 06 '19 02:10 oxycoder

I can confirm it works well under on Docker with Linux host (Manjaro distro).

julienkosinski avatar Nov 15 '19 14:11 julienkosinski

I can confirm it works well under on Docker with Linux host (Manjaro distro).

Which storage driver do you happen to be using? With @nathany's comment in mind, this could be filesystem dependent. In both 'working' cases, this sounds like a bind mount, not a K8's storage driver under the PVC subsystem

jnach avatar Jan 06 '20 18:01 jnach

This seems to do a good job of explaining the phenomenon here..

jnach avatar Jan 06 '20 18:01 jnach

Same problem, any solutions?

Sorta, see above. TL&DR This is a file-system compatibility issue. There are a few scenarios where it works when using the same underlying host filesystem, but pretty much never if there is any translation.

jnach avatar Jan 06 '20 18:01 jnach

I'm also interested in a workaround or solution to this, if there's any. Would be ok with polling if that does the job. Could be a CLI option

reyronald avatar May 20 '20 15:05 reyronald

Me too,

Is there any certain plan to cover this issue?

There are several packages using fsnotify (eg: air, compileDaemon ...), I have tried them all, and these packages have the same issue, certainly.

Hanggi avatar Sep 25 '20 02:09 Hanggi

Closed as duplicate of #9; if the underlying OS mechanism doesn't work then there's not much we can do about it other than implement a polling fallback.

arp242 avatar Oct 14 '22 17:10 arp242