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?
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
--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.
Is there any possibility that the Docker image you're looking at is the same one that was initially created before you made the changes? I think what might be happening is that the image isn't being rebuilt to reflect the file changes you're making and nodemon is just restarting the exact same container that was built initially or the container you're observing in your browser is not being restarted or updated at all for some reason. If you have your text editor side by side to your browser, you should be able to save file changes and refresh the browser to see your changes take effect immediately.
The first way you could tell for sure is if you update the tag on the container you're watching and modifying using nodemon with docker image tag your-current-image-name:0.0.0
then add LABEL org.opencontainers.image.title="your-current-image-name"
and LABEL org.opencontainers.image.version="0.0.1"
directly under FROM node:14-alpine as base
in your Dockerfile then rebuild the container with docker-compose down
, docker-compose build
, docker-compose up -d
then docker image ls
to make sure your new version (0.0.1) was created properly and the old one (0.0.0) is still there.
I know it's not ideal and counterintuitive to using nodemon but you could also use the nuclear option in order to test the theory that nodemon is restarting the exact same Docker container over and over again or not changing it at all. You would have to run docker-compose down
then remove all the images with docker image prune --all --force
but make sure you don't need to tag and send any of the other images you have to Docker Hub before you do this because it's going to erase all of your containers.
You could also try setting USER root
below ENV NODE_ENV=development
and then put USER node
under ENV NODE_ENV=production
in your Dockerfile just to make sure it isn't some type of permissions issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.