gpt4 book ai didi

javascript - 由于绑定(bind)安装,Dockerfile 和 docker-compose 找不到节点模块

转载 作者:搜寻专家 更新时间:2023-11-01 04:21:38 26 4
gpt4 key购买 nike

So I have this NodeJS express app. This is code works:

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

But for this app I want to see the live code changes. So I know that is where you can use Bind Mounts, but as soon as I add the volumes like so:

docker-compose.yml

version: "3.7"

services:
node-express:
build: .
volumes:
- .:/app
ports:
- 80:3000

It looks like the node_modules is not in the "app" directory anymore.. How can I fix this? I've worked on this for four hours, and I still can't spot the mistake!

错误

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

查看工作树: enter image description here

在我将卷添加到 docker-compose.yml 之前,容器运行成功。这是结构: enter image description here

最佳答案

您需要修改您的项目结构,将每个可编辑的代码移动到一个文件夹中(比方说 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 也进行适当的更改(目录结构已更改)

关于javascript - 由于绑定(bind)安装,Dockerfile 和 docker-compose 找不到节点模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com