简体   繁体   English

Windows 上带有 Docker 的 NodeJs - Nodemon 没有更新

[英]NodeJs with Docker on Windows - Nodemon no update

I'm trying to make nodemon work with docker so the server restarts after every change, but I can't seem to make it work.我试图让 nodemon 与 docker 一起工作,以便服务器在每次更改后重新启动,但我似乎无法让它工作。

Dockerfile文件

FROM node:14

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]

package.json包.json

    "main": "server.ts",
    "scripts": {
        "build": "tsc -p .",
        "start": "nodemon -L src/server.ts"
    },
    "dependencies": {
        "express": "^4.17.1",
        "ts-node": "^8.6.2",
        "typescript": "^3.7.5"
    },
    "devDependencies": {
        "@types/node": "^13.7.0",
        "eslint": "^6.8.0",
        "nodemon": "^2.0.12"
    }

If I run the server locally, nodemon works, but through Docker it does not (it just runs once).如果我在本地运行服务器,则 nodemon 可以工作,但通过 Docker 则不行(它只运行一次)。 Do you have any idea how to solve it?你知道如何解决它吗?

Using nodemon in Docker containers doesn't make sense.在 Docker 容器中使用nodemon没有意义。 The reason is whenever you change some code you need to do a docker build to make that into an image and then run that image as container.原因是每当您更改某些代码时,您都需要进行docker build以将其转换为映像,然后将该映像作为容器运行。

So, the last container actually stops and a new container starts each time you want to change a code.因此,每次您想要更改代码时,最后一个容器实际上会停止,并且会启动一个新容器。 It is like you stopping node and running again.就像您停止node并再次运行一样。

There may be a scenario when you mount your code from host machine to the container then running nodemon on the mountpoint would probably be a fair choice.当您将代码从主机挂载到容器然后在挂载点上运行nodemon可能是一个公平的选择时,可能会出现这样的情况。 But for your dockerfile it isn't.但是对于您的 dockerfile 来说不是。

What is the run command of your docker container?你的 docker 容器的运行命令是什么?
If you need docker container see the change you make at local, you should mount the volumes from host(local) to container like this:如果您需要 docker 容器查看您在本地所做的更改,您应该将卷从主机(本地)挂载到容器,如下所示:

docker run -dp 8080:8080 -v $(pwd):/usr/src/app

with $(pwd) for your current working dir(local)使用$(pwd)作为您当前的工作目录(本地)
Or if you use docker-compose, mount volumes like this:或者,如果您使用 docker-compose,请像这样安装卷:

services:
  your_app:
    build:
      context: .
    volumes: # mount volumes here
      - ./:/usr/src/app
# rest config...

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

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