简体   繁体   English

开发环境NodeJS和Docker

[英]Development env NodeJS and Docker

I created the following Dockerfile 我创建了以下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 当我构建和运行Docker映像时,一切正常

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? 如何配置Dockerfile以动态支持代码更改?

The problem you're facing is that you declare a volume on your container's /usr/src/app/ folder. 您面临的问题是您在容器的/usr/src/app/文件夹中声明了一个卷。 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. 它的作用是用文件系统上的那个文件夹替换了您容器的文件夹,该文件系统当然没有执行npm install命令。

As if, your Dockerfile is valid, and you can distribute it like that. 好像您的Dockerfile是有效的,您可以像这样分发它。 But for local development purpose, you can't have the npm install be run on the image (Dockerfile) itself. 但是出于本地开发目的,您不能让npm install在映像(Dockerfile)本身上运行。 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. 因此,您只需在容器启动时在本地~/projects/NodeJSExample/上运行npm install命令,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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