简体   繁体   English

使用Windows 10和Nodemon进行Docker热加载节点应用

[英]Docker hot reloading node app with nodemon using Windows 10

I have an express app and I'm trying to setup hot reloading with nodemon using docker for windows 10 as a dev environment. 我有一个快速应用,我正在尝试使用docker for Windows 10作为开发环境,使用nodemon设置热重载。 However when I npm install on the volume it doesn't seem to work. 但是,当我在卷上安装npm时,它似乎不起作用。

With powershell I use a volume like so: 使用powershell时,我会像这样使用卷:

docker build -t node-api . docker build -t node-api。

docker run --rm -it -p 8080:8080 -v "${PWD}:/usr/src/app" node-api docker run --rm -it -p 8080:8080 -v“ $ {PWD}:/ usr / src / app” node-api

Which outputs this error: 哪个输出此错误:

[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
    at Function.Module._load (internal/modules/cjs/loader.js:529:25)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/usr/src/app/server/server.js:1:79)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
[nodemon] app crashed - waiting for file changes before starting...

If I exclude the -v flag and docker run then it starts with no errors, but doesn't detect changes or restart on file saves. 如果我排除-v标志并运行docker,则它不会出现任何错误,但不会检测到更改或在文件保存时重新启动。

Dockerfile Docker文件

FROM node:alpine

#Create app directory
WORKDIR /usr/src/app

#Install nodemon for hot reloading
RUN npm install nodemon -g

#Install app dependencies
#A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . .

EXPOSE 8080
CMD [ "nodemon", "-L", "server/server.js" ]

Folder Structure 资料夹结构

server/

    >server.js

Dockerfile

package.json

Github repo for code Github回购代码

The problem is that by mounting the whole host app/ directory as volume, the app/node_modules/ inside the container would be overwritten by the host's app/ , thus all dependencies are missing. 问题在于,通过将整个主机app/目录作为卷安装,容器内的app/node_modules/将被主机的app/覆盖,因此所有依赖项都将丢失。

The solution is to only mount the source code folder you need, ie 解决方案是仅挂载所需的源代码文件夹,即

docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api

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

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