Within Docker VM, Gulp-Watch Seems to not work well on volumes hosted from the host OS
Asked Answered
G

6

13

So I have a setup, probably as most people have, where their app code is mounted into a Docker container through a separate volume.

The problem is that if I run gulp, and specifically gulp-watch, to watch for file modifications etc. within docker, on the app code mounted within the docker container, to properly build and restart node within the docker container as necessary, it seems to get cpu intensive (as in polling for file changes instead of listening for file change events) to the point where my machine buckles.

I think this is due to a limitation of having the file system mounted from the native host to the docker container but how are folks working around this? Are they doing all of their work in the container? Native host then constantly building? Or am I missing something where my setup is incorrect with gulp-watch / nodemon?

Guerrero answered 23/2, 2015 at 19:8 Comment(2)
It seems related to this, if folks have any input, github.com/paulmillr/chokidar/issues/212Guerrero
More closely related to github.com/paulmillr/chokidar/issues/242, which was active and getting close to a solution as of time of writing. Workaround: just run gulp watch locally and let your docker container serve the static files. If you depend on livereload, you'll likely have the same problem there, but give it a few weeks/months :)Birdbath
E
11

For anyone using gulp4

The only way I could get this to work is to use usePolling like below

gulp.watch('./**/*', {interval: 1000, usePolling: true}, gulp.series('superTask'));
Elliotelliott answered 10/5, 2016 at 10:38 Comment(1)
As mentioned in the docs (gulpjs.com/docs/en/api/watch) usePolling is the correct option: needed for successfully watching files over a network or other non-standard situationsMantua
J
6

Try changing the gulp.watch options. This has been much better for me:

gulp.watch('./**/*', {interval: 1000, mode: 'poll'}, ['build']);
Judiejudith answered 25/6, 2015 at 17:42 Comment(0)
G
4

You should use the plugin gulp-watch instead of gulp.watch. The latter uses stat polling, which is much too heavy for the shared file system. gulp-watch uses inotify events to watch the file system on OSX.

Guttering answered 16/1, 2017 at 16:55 Comment(0)
M
1

The previous answer of usePoll: true didn't work. This one did:

gulp.watch('./**/*', {interval: 1000, usePolling: true}, ['build']);
Maraschino answered 2/7, 2019 at 16:28 Comment(0)
N
0

Jesse's answer didn't work for me, but it was really close. Now, the option seems to be:

gulp.watch('./**/*', {interval: 1000, usePoll: true}, ['build']);

The mode field has been switched out for the usePoll field flag.

See the API section for more details.

Nervous answered 25/11, 2015 at 1:47 Comment(0)
Z
0

In a docker container that has a nodemon installed (npm i -g nodemon) there is an alternative to gulp watch.

Let's say that one wants to watch changes to a swagger.yaml file in ./swagger/swagger.yaml and convert it to a project.json file for use with swagger UI.

Assuming that the correct node modules are installed, or that a stand-alone yaml to json convert tool is installed, one could run the following:

nodemon -L --watch ./editor/api/swagger/* --exec "node ./cvt_yaml_to_json.js"

where:

  • ./editor/api/swagger/* is the directory to watch for file changes
  • "node ./cvt_yaml_to_json.js" is the command to execute (it can be an arbitrary command). In this case it is a JavaScript script which depends on js-yaml module (npm i js-yaml) and performs YAML to JSON conversion like this:
const yaml = require("js-yaml");
const path = require("path");
const fs = require("fs");

const swaggerYamlFile = "/api/project/editor/api/swagger/swagger.yaml";
const swaggerJsonFile = "/api/project/project.json";

//Converts yaml to json
const doc = yaml.safeLoad(fs.readFileSync(swaggerYamlFile));
fs.writeFileSync(swaggerJsonFile, JSON.stringify(doc, null, " "));

Zelikow answered 25/6, 2020 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.