简体   繁体   中英

How to mount my local source file to docker container?

I am new to docker and I have created my custom image.

Here are my Dockerfile

FROM node:latest

MAINTAINER FF

COPY . /var/www

WORKDIR /var/www

RUN npm install  

EXPOSE 3000

ENTRYPOINT ["npm", "start"]

I then build an image with

docker build -t test/node .

After the image is built, I ran

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

I use -v because I wanted to mount my host src folder to the /var/www .

It came back as

> 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

I think the issue is my src folder doesn't have express module but I already did npm install in my image and I thought that's all I need.

What did I do wrong? Can anyone help me about it? Thanks so much!

The problem is the combination of COPY and volume

What's happening

  1. You copy the source code to Docker image at the time of image creation
  2. npm install runs inside the image (thus not affecting your source code)
  3. But, when you mount the volume, it overrides the /var/www effectively making steps 1. and 2. useless

What you want to do is run npm install either before starting docker or inside it.

You created an image where you ran the npm install on the /var/www folder, inside the image. This install did not happen on your host filesystem.

Then you used that image to spin up a container and mounted your host filesystem to /var/www. That overlays everything that exists at that location in the image, so prior installs you've done at that location are not visible unless you've run them on your host.

Your options include:

  • Be more selective about which files/folders you mount into your container, perhaps all your code goes into a subdirectory and you mount that.
  • Perform the npm install on your host. You can do this as part of the container startup so that the executables are inside the container if you want.
  • Rebuild the container for each change you make. Not ideal, but you can restructure the Dockerfile commands so that the most frequently changes are copied only at the end so that docker can cache the previous layers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM