Nodemon 没有在我的 docker 容器上重新加载我的 app.js

Nodemon doesn't reload my app.js on my docker container

我决定在容器中创建一个带有 Express 的服务器,并安装了 nodemon 来监视和重新加载我的代码修改,但是由于某种原因,当我修改代码时,容器上的 nodemon 没有重新加载。我该如何解决?

我的 Dockerfile:

FROM node:14-alpine

WORKDIR /usr/app

RUN npm install -g nodemon

COPY . .

EXPOSE 3000

CMD ["npm","start"]

我的package.json:

{
  "name": "prog-web-2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/willonf/prog-web-2.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/willonf/prog-web-2/issues"
  },
  "homepage": "https://github.com/willonf/prog-web-2#readme",
  "dependencies": {
    "express": "^4.17.1"
  }
}

我的app.js:

const express = require("express")

const app = express()

app.get("/", (req, res) => {
    res.end("Hello, World!")
});

app.listen(3000);

您正在复制所有文件到docker容器中。

COPY . .

因此,当您修改本地文件时,您并没有修改 docker 容器内的文件,并且 docker 容器内的 nodemon 无法检测到任何更改。

使用 Docker Volumes 可以获得您想要的行为。您可以配置它们,以便 docker 容器 与主机系统共享 工作目录。如果您更改主机上的文件,nodemon 会在这种情况下检测到更改。

this post 中的答案显示了如何完成该操作的示例。