所以我有这个NodeJS Express应用程序。这是代码的工作原理:
Dockerfile
FROM node:10.16-alpine
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]
docker-compose.yml
version: "3.7"
services:
node-express:
build: .
ports:
- 80:3000
但是对于这个应用程序,我想查看实时代码更改。所以我知道 是可以使用“绑定挂载”的位置,但是在我添加卷后 像这样:
docker-compose.yml
version: "3.7"
services:
node-express:
build: .
volumes:
- .:/app
ports:
- 80:3000
似乎node_modules不在“ app”目录中。 我怎样才能解决这个问题?我已经为此工作了四个小时,但我仍然 无法发现错误!
错误
HijackManiac:webserver hijackmaniac$ docker-compose up
Creating network "webserver_default" with the default driver
Building node-express
Step 1/5 : FROM node:10.16-alpine
---> a298c79121d9
Step 2/5 : WORKDIR /app
---> Running in 9b99df0f54ba
Removing intermediate container 9b99df0f54ba
---> 48f9b606b32d
Step 3/5 : COPY . /app
---> 1f29dc735dc7
Step 4/5 : RUN npm install
---> Running in 81225f56a9fd
npm WARN webserver@1.0.0 No description
npm WARN webserver@1.0.0 No repository field.
added 106 packages from 130 contributors and audited 202 packages in 3.344s
found 0 vulnerabilities
Removing intermediate container 81225f56a9fd
---> 2985dc77bc12
Step 5/5 : CMD ["npm", "start"]
---> Running in 271ab42687ce
Removing intermediate container 271ab42687ce
---> 38a39768792e
Successfully built 38a39768792e
Successfully tagged webserver_node-express:latest
WARNING: Image for service node-express was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating webserver_node-express_1 ... done
Attaching to webserver_node-express_1
node-express_1 |
node-express_1 | > webserver@1.0.0 start /app
node-express_1 | > node src/app.js
node-express_1 |
node-express_1 | internal/modules/cjs/loader.js:638
node-express_1 | throw err;
node-express_1 | ^
node-express_1 |
node-express_1 | Error: Cannot find module 'express'
node-express_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
node-express_1 | at Function.Module._load (internal/modules/cjs/loader.js:562:25)
node-express_1 | at Module.require (internal/modules/cjs/loader.js:690:17)
node-express_1 | at require (internal/modules/cjs/helpers.js:25:18)
node-express_1 | at Object.<anonymous> (/app/src/app.js:2:17)
node-express_1 | at Module._compile (internal/modules/cjs/loader.js:776:30)
node-express_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
node-express_1 | at Module.load (internal/modules/cjs/loader.js:653:32)
node-express_1 | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
node-express_1 | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
node-express_1 | npm ERR! code ELIFECYCLE
node-express_1 | npm ERR! errno 1
node-express_1 | npm ERR! webserver@1.0.0 start: `node src/app.js`
node-express_1 | npm ERR! Exit status 1
node-express_1 | npm ERR!
node-express_1 | npm ERR! Failed at the webserver@1.0.0 start script.
node-express_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
node-express_1 | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
node-express_1 |
node-express_1 | npm ERR! A complete log of this run can be found in:
node-express_1 | npm ERR! /root/.npm/_logs/2019-06-04T23_55_20_327Z-debug.log
webserver_node-express_1 exited with code 1
答案 0 :(得分:2)
将项目卷装载到容器工作目录中时,您将覆盖自从Dockerfile中原始复制/添加以来您所做的所有更改,以及本地项目目录中的内容。在这种情况下,这意味着node_modules文件夹。我发现解决此问题的最简单方法是不挂载整个项目目录,而只是挂载具有我的源代码的子文件夹。
答案 1 :(得分:1)
您需要修改项目结构,以将每个可编辑的代码移至src
文件夹中,或更新Dockerfile,以将node_modules
移至上一级目录。
我们知道,当Node在当前目录中找不到node_modules时,它开始在父目录中寻找它,因此我们可以利用它。
与此类似的Dockerfile将解决您的问题。
FROM node:10.16-alpine
WORKDIR /node
COPY package.json package-lock.json ./
RUN npm install
WORKDIR /node/app
COPY . .
CMD ["npm", "start"]
请确保对docker-compose.yml也进行适当的更改(目录结构已更改)