gpt4 book ai didi

node.js - 如何将我的本地源文件挂载到 docker 容器?

转载 作者:搜寻专家 更新时间:2023-11-01 00:29:34 25 4
gpt4 key购买 nike

我是 docker 的新手,我已经创建了我的自定义镜像。

这是我的 Dockerfile

FROM node:latest

MAINTAINER FF

COPY . /var/www

WORKDIR /var/www

RUN npm install

EXPOSE 3000

ENTRYPOINT ["npm", "start"]

然后我构建一个图像

docker build -t test/node .

镜像构建完成后,我运行

docker run -p 8888:3000 -v $(pwd):/var/www -w "/var/www" test/node

我使用 -v 是因为我想将主机 src 文件夹挂载到 /var/www

它回来了

> expresssite@0.0.0 start /var/www
> node ./bin/www

module.js:472
throw err;
^

Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/app.js:1:77)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)

npm info lifecycle expresssite@0.0.0~start: Failed to exec start script
npm ERR! Linux 4.4.39-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v7.4.0
npm ERR! npm v4.0.5
npm ERR! code ELIFECYCLE
npm ERR! expresssite@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the expresssite@0.0.0 start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the expresssite package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs expresssite
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls expresssite
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! Please include the following file with any support request:
npm ERR! /var/www/npm-debug.log

我认为问题是我的 src 文件夹没有 express 模块,但我已经在我的图像中安装了 npm,我认为这就是我所需要的。

我做错了什么?谁能帮我解决这个问题?非常感谢!

最佳答案

问题是COPY和volume的结合

发生了什么

  1. 您在创建镜像时将源代码复制到 Docker 镜像
  2. npm install 在图像内部运行(因此不会影响您的源代码)
  3. 但是,当您装载该卷时,它会覆盖/var/www 有效地使步骤 1. 和 2. 无用

您要做的是在启动 docker 之前或在其中运行 npm install。

关于node.js - 如何将我的本地源文件挂载到 docker 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42045649/

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