我是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文件夹没有快递模块,但我已经在我的图像中安装了npm,我认为这就是我所需要的。
我做错了什么?任何人都可以帮我吗?非常感谢!
答案 0 :(得分:2)
问题是COPY和音量的组合
发生了什么
你想要做的是在启动docker之前或在其中运行npm install。
答案 1 :(得分:2)
您创建了一个图像,您可以在图像内的/ var / www文件夹中运行npm install。您的主机文件系统上没有发生此安装。
然后,您使用该图像旋转容器并将主机文件系统挂载到/ var / www。这会覆盖图像中该位置的所有内容,因此除非您在主机上运行,否则之前在该位置完成的安装将不可见。
您的选择包括: