简体   繁体   中英

Development env NodeJS and Docker

I created the following Dockerfile

ROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

Everything works fine when I build and run the Docker image

However, when I run

docker run -p 8080:8080 -v ~/projects/NodeJSExample/:/usr/src/app/ nodeexample

I got :

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/usr/src/app/server.js:3:17)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)

How can I configure Dockerfile to support code changes dynamically?

The problem you're facing is that you declare a volume on your container's /usr/src/app/ folder. What it does is that it replaces your container's folder by the one on your filesystem, which certainly did not have the npm install command executed.

As if, your Dockerfile is valid, and you can distribute it like that. But for local development purpose, you can't have the npm install be run on the image (Dockerfile) itself. So you only need to run the npm install command on your local ~/projects/NodeJSExample/ while your container is up, and you're good to go.

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