我有一个快速应用,我正在尝试使用docker for Windows 10作为开发环境,使用nodemon设置热重载。但是,当我在卷上进行npm安装时,它似乎无法正常工作。
借助powershell,我可以像这样使用一个卷:
docker build -t node-api。
docker run --rm -it -p 8080:8080 -v“ $ {PWD}:/ usr / src / app” node-api
哪个输出此错误:
[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...
如果我排除-v标志并且docker run,则它不会出现任何错误,但不会检测到更改或重新启动文件保存。
Dockerfile
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" ]
文件夹结构
server/
>server.js
Dockerfile
package.json
Github repo获取代码
答案 0 :(得分:1)
问题在于,通过将整个主机app/
目录安装为卷,容器中的app/node_modules/
将被主机的app/
覆盖,因此所有依赖项都将丢失。
解决方案是仅挂载所需的源代码文件夹,即
docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api