简体   繁体   English

Nodemon 不使用 express 在 docker 中更改时重新加载文件

[英]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.我用 express-generator 生成了一个项目,并且正在对各种文件进行更改 - 从 app.js 到路由,没有任何内容会导致 nodemon 更新。 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).这一切都在一个 docker 容器中,它正确显示了文件更改(我已经监视了 docker shell 中的文件,以确保 docker 正在更新它们,确实如此)。

My app.js and bin/www files are standard express-generator files.我的 app.js 和 bin/www 文件是标准的 express-generator 文件。

package.json: 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.我试过 nodemon -L./bin/www,没有 -L,指定完整的文件系统路径 (/src/) 和其他一些东西,nodemon 只是不监视更改。

Dockerfile-node: Dockerfile节点:

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):相关的 docker-compose.yml 部分(使用 docker-compose 版本 3.8):

   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: Docker output 为节点容器:

[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 src bin而不是--watch . ./bin --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).我认为nodemon内部使用glob或其他东西来解析该路径,如果您使用./它会破坏它(我不确定)。

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.也不要在 root 上查看所有可能的文件,指定您实际想要查看的目录,否则您会在不必要的事情上添加大量额外的递归查看。

https://github.com/remy/nodemon#monitoring-multiple-directories . 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 .进一步阅读他们的文档,他们还提到在某些情况下使用带有挂载驱动器的容器可能会导致问题,然后他们建议使用--legacy-watch标志,然后在内部使用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.我相信您的问题是[nodemon] watching path(s): *.*./解析不正确,并尝试在所有路径上观看。

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.还有一点需要注意的是,nodemon 将在当前工作目录中运行,因此如果您使用--watch src bin确保您在运行它时实际上位于项目根目录中。

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?您正在查看的 Docker 镜像是否有可能与您在进行更改之前最初创建的镜像相同? 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.我认为可能发生的情况是图像没有被重建以反映您所做的文件更改,而 nodemon 只是重新启动了最初构建的完全相同的容器,或者您在浏览器中观察到的容器不是出于某种原因重新启动或更新。 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.您可以确定的第一种方法是,如果您使用 nodemon 和 docker docker image tag your-current-image-name:0.0.0更新正在监视和修改的容器上的标签,然后添加LABEL org.opencontainers.image.title="your-current-image-name"LABEL org.opencontainers.image.version="0.0.1"直接在FROM node:14-alpine as base Dockerfile 中的FROM node:14-alpine as base ,然后使用docker-compose build docker-compose downdocker-compose build重建容器docker-compose builddocker-compose up -d然后docker image ls以确保您的新版本(0.0.1)已正确创建并且旧版本(0.0.0)仍然存在。

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.我知道使用 nodemon 并不理想和违反直觉,但您也可以使用核选项来测试 nodemon 一遍又一遍地重新启动完全相同的 Docker 容器或根本不更改它的理论。 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.您必须运行docker-compose down然后使用docker image prune --all --force删除所有图像,但请确保在执行此操作之前不需要标记和发送任何其他图像到 Docker Hub因为它会清除你所有的容器。

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.您还可以尝试在ENV NODE_ENV=development下设置USER root ,然后将USER node放在ENV NODE_ENV=production ,以确保它不是某种类型的权限问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM