nodemon in Docker: legacy polling vs native file events for mounted volumes
0 reputation · 04 Jun 2023, 18:35 UTC
0 reputation · 04 Jun 2023, 18:35 UTC
I'm running a Node.js app inside a Docker container with the source directory bind-mounted from the host, and I use nodemon for automatic restarts during development. I need to pick a watching strategy that stays reliable without making the dev container unusable.
From the documentation, nodemon defaults to native OS file system events, which are fast and cheap, but those events are reported to be unreliable when a volume is mounted from a Windows or macOS host. The alternative is the -L / --legacy-watch polling mode, which checks timestamps on an interval and works on networked or virtualized file systems, at the cost of higher CPU usage that grows with project size.
My constraints: the project tree is fairly large (including node_modules, which I can exclude via nodemon.json), and several developers use different host operating systems, so the choice has to live in shared configuration rather than per-machine flags.
Given a large bind-mounted project, is legacy polling with a tuned interval and ignore list the accepted trade-off, or is there a documented way to keep native events working across hosts? What polling interval is a reasonable starting point before CPU overhead becomes a real problem?
32275 reputation · 04 Jun 2023, 21:15 UTC
For a large bind‑mounted project that is shared across Windows, macOS and Linux hosts, the reliable, documented solution is to enable legacyWatch (the --legacy-watch flag). There is no flag that guarantees native file‑system events will fire correctly on all hosts when the source is mounted from a Windows or macOS machine, because Docker’s file‑system drivers translate events in a way that can lose or delay them.
Thus, the accepted trade‑off is: use legacy polling with an ignore list for node_modules and, if needed, a tuned polling interval. A starting interval of 2000 ms (the nodemon default) is safe; lowering to 1000 ms gives faster restarts with only modest CPU impact. Intervals below 500 ms tend to spike CPU usage in containers that already have many watchers.
ignore patterns to reduce the number of files polled.Place a nodemon.json in the project root (or use CLI flags). Example configuration:
{
"watch": ["src"],
"ignore": ["node_modules"],
"legacyWatch": true,
"watchDelay": 2000
}
Or run nodemon with flags:
nodemon --legacy-watch --watch-delay 2000 --watch src
watchDelay of 2000 ms.docker stats or top). If the CPU stays below your threshold, you can reduce the interval.watchDelay 1000 ms: responsiveness improves, CPU usually stays acceptable.Do you have a CPU usage limit you need to stay under when using polling? Knowing that threshold can help fine‑tune the interval.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.