[英]How to mount my local source file to docker container?
我是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和音量的結合
發生了什么
您想要做的是在啟動docker之前或在docker內部運行npm install。
您在映像內的/ var / www文件夾中運行npm安裝的位置創建了映像。 此安裝未在主機文件系統上進行。
然后,您使用該映像啟動了一個容器,並將主機文件系統安裝到/ var / www。 這會覆蓋映像中該位置上存在的所有內容,因此除非您在主機上運行它們,否則看不到您在該位置上完成的先前安裝。
您的選擇包括:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.