Jon
Jon

Reputation: 385

Nodemon not reloading files on change in docker with express

I generated a project with express-generator, and am making changes to various files - from app.js to the routes, and nothing causes nodemon to update. It's all in a docker container which is showing file changes properly (I've monitored the files in the docker shell to make sure docker is updating them, and it is).

My app.js and bin/www files are standard express-generator files.

package.json:

{
  "name": "api",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "dev": "nodemon -L --watch . ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1"
  }
}

I've tried nodemon -L ./bin/www, without -L, specifying the full filesystem path (/src/), and a few other things and nodemon just does not monitor changes.

Dockerfile-node:

FROM node:14-alpine as base

WORKDIR /src
COPY ./API/package*.json /src/
EXPOSE 3000

FROM base as production
ENV NODE_ENV=production
RUN npm ci
COPY ./API/ /src
CMD ["npm", "run", "start"]

FROM base as dev
ENV NODE_ENV=development
RUN npm install -g nodemon && npm install
COPY ./API/ /src
CMD ["npm", "run", "dev"]

Relevant docker-compose.yml portion (Using version 3.8 of docker-compose):

   api:
      build:
        context: ./
        dockerfile: Dockerfile-node
        target: dev
      container_name: API
      depends_on:
        - db
      restart: always
      volumes:
        - ./API:/src
      #command: npm run dev
      env_file: ./.env
      environment:
        DB_HOST: db
        DB_PORT: 3306
        DB_NAME: $DB_NAME
        DB_USER: $DB_USER
        DB_PASSWORD: $DB_PASSWORD
        NODE_ENV: development
        DEBUG: nodejs-docker-express:*
      ports:
        - "3000:3000"
      stdin_open: true
      tty: true

Docker output for the node container:

[nodemon] 2.0.14

[nodemon] to restart at any time, enter `rs`

[nodemon] watching path(s): *.*

[nodemon] watching extensions: js,mjs,json

[nodemon] starting `node bin/www`

What am I doing wrong?

Upvotes: 2

Views: 2121

Answers (1)

user16435030
user16435030

Reputation:

I believe you should not be using directory paths the way you're doing, just use the directory name directly. --watch src bin instead of --watch . ./bin. I think nodemon internally uses glob or something to resolve that path and if you use ./ it will break it (I'm not sure about that).

Also don't watch all possible files on root, specify the directories you actually want to watch, otherwise you're adding a lot of additional recursive watching on unnecessary things.

https://github.com/remy/nodemon#monitoring-multiple-directories.

Reading further down their documentation, they also mention that in some cases using containers with a mounted drive can cause issues and they then recommend using the --legacy-watch flag, which will internally then use chokidar. That's something you can try if fixing the path name doesn't work.

I believe your issue is [nodemon] watching path(s): *.*, the ./ is resolving incorrectly and tries to watch on all paths or something.

Something else to note is that nodemon will run in the current working directory, so if you are using --watch src bin make sure you're actually in the project root when you run that.

Upvotes: 2

Related Questions