36

My goal is to set up a Docker container that automatically restarts a NodeJS server when file changes are detected from the host machine.

I have chosen nodemon to watch the files for changes.

On Linux and Mac environments, nodemon and docker are working flawlessly.

However, when I am in a Windows environment, nodemon doesn't restart the server.

The files are updated on the host machine, and are linked using the volumes parameter in my docker-compose.yml file.

I can see the files have changed when I run docker exec <container-name> cat /path/to/fileChanged.js. This way I know the files are being linked correctly and have been modified in the container.

Is there any reason why nodemon doesn't restart the server for Windows?

RyanNHG
  • 1,009
  • 2
  • 8
  • 20

5 Answers5

77

Use nodemon --legacy-watch to poll for file changes instead of listening to file system events.

VirtualBox doesn't pass file system events over the vboxfs share to your Linux VM. If you're using Docker for Windows, it would appear HyperV doesn't propagate file system events either.

As a 2021 side note, Docker for Mac/Windows new GRPCfuse file system for mounting local files into the VM should send file system events across now.

2022 note: Looks like Windows/WSL Docker doesn't share FS events to the Linux VM (see comments @Mohamed Mirghani and @Ryan Wheale and github issue).

Matt
  • 68,711
  • 7
  • 155
  • 158
  • 2
    Thank you so much! I just checked out this section in the [Nodemon README](https://github.com/remy/nodemon#application-isnt-restarting). I appreciate your incredible response time. – RyanNHG Aug 31 '16 at 14:42
  • You can also specify a polling time: Just add this to the nodemon.json config file: "legacyWatch": true, "pollingInterval": 4000 – Sandokan El Cojo Oct 11 '17 at 10:15
  • 2
    For the newcomers and such: a command for this from your Dockerfile is: `CMD ["nodemon", "-L", "your_file_here"]`. Or similarly you can put something like this in your Dockerfile: `CMD ["npm", "run", "custom_script_name"]` and then in `package.json` put `"scripts": { "custom_script_name": "nodemon -L your_file_here", ... }` Either of those will run this command from your Dockerfile after `docker-compose up`. – Augie Gardner Jan 05 '18 at 08:13
  • 2
    Thanks so much, side note: it is 2022 and it seems like windows doesn't send files system events to the VM – Mohamed Mirghani Jan 29 '22 at 07:40
  • 1
    Agreed with @MohamedMirghani - it's Feb. 2022, I'm on the latest docker for Windows (with WSL backend) and changes from a Windows filesystem mounted in a Linux container are not being picked up by file watchers in the container. – Ryan Wheale Jan 31 '22 at 22:58
  • My estimate is that this will be fixed in 2025. – Rok Sprogar Jul 16 '22 at 17:20
12

It is simple, according to the doc you must change:

nodemon server.js

to:

nodemon --legacy-watch server.js
Masih Jahangiri
  • 9,489
  • 3
  • 45
  • 51
4

As mentioned by others, using node --legacy-watch will work, however, the default polling rate is quite taxing on your cpu. In my case, it was consuming 30% of my CPU just by looping through all the files in my project. I would advise you to specify the polling interval as mention by @Sandokan El Cojo.

You can do so by either adding "pollingInterval": 4000 (4 seconds in this example) to your nodemon.json file or specifying it with the -P or --polling-interval flag in the command.

Vladimort
  • 91
  • 1
  • 6
  • 1
    btw the default is 100ms. I figured that's way too quick and eating too many resources so 1000 should be totally acceptable. It unsurprisingly scales linearly, and my cpu usage on a node container wen tfrom 60% usage to 6%. https://github.com/remy/nodemon/blob/0823f18435e43639f3b6b9f296b7385e99cffa3b/doc/cli/options.txt#L25 – 1mike12 Nov 02 '22 at 21:44
0

This was an issue in the docker for Windows. Now it's fixed

https://www.docker.com/blog/new-filesharing-implementation-in-docker-desktop-windows/

Mukesh Agarwal
  • 392
  • 4
  • 13
0

I had a very similar trouble within my docker environment, but the problem was the volumes were not correctly set, so please check always you are really editing the files that nodemon is trying to watch.

Alejandro Giraldo
  • 609
  • 1
  • 6
  • 11