生产与开发Docker设置Node(Express&Mongo)应用程序

我试图将Node应用程序转换为使用Docker,但遇到了一些我无法回答的问题/问题。

但为了简单,我已经包含了一些非常基本的示例文件,以便将问题保持在目标之上 实际上下面的例子只是链接到一个Mongo容器,但并没有在代码中使用它来保持它更简单。

首先, DockerfileDockerfile docker-compose.yml设置是否需要在本地(OS X)开发和生产版本上成功使用Node + Express + Mongo应用程序上的Docker?

Dockerfile

 FROM node:6.3.0 # Create new user to avoid using root - is this correct practise? RUN useradd --user-group --create-home --shell /bin/false app COPY package.json /home/app/code/ RUN chown -R app:app /home/app/* USER app WORKDIR /home/app/code # Should this even be set here or use docker-compose instead? # And should there be: # - docker-compose.yml setting it to production by default # - docker-compose.dev.yml setting it to production? # Or reverse it? (docker-compose.prod.yml instead with default being development?) # Commenting below out or it will always run as production #ENV NODE_ENV production RUN npm install USER root COPY . /home/app/code # Running chown to ensure new 'app' user owns files RUN chown -R app:app /home/app/* USER app EXPOSE 3000 # What CMD should be here to ensure development versus production is simple? # Development - Restart server and refresh browser on file changes # Production - Ensure uptime. CMD ["npm", "start"] 

泊坞窗,compose.yml

 version: "2" services: web: build: . # I would normally use a .env file but for this example will set explicitly # env_file: .env environment: - NODE_ENV=production volumes: - ./:/home/app/code - /home/app/code/node_modules ports: - "3000:3000" links: - mongo mongo: image: mongo ports: - "27017:27017" 

泊坞窗,compose.dev.yml

 version: "2" services: web: # I would normally use a .env file but for this example will set explicitly # env_file: .env environment: - NODE_ENV=development 

的package.json

 { "name": "docker-node-test", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "start": "nodemon app.js" }, "dependencies": { "express": "^4.14.0", "mongoose": "^4.6.1", "nodemon": "^1.10.2" }, "devDependencies": { "mocha": "^3.0.2" } } 

1.如何处理不同的NODE_ENV(dev,production,staging)?

这是我的主要问题和难题。

在我使用的例子中,NODE_ENV在Dockerfile中被设置为production并且有两个docker-compose文件:

  • docker-compose.yml设置默认包括NODE_ENV到production
  • docker-compose.dev.yml覆盖NODE_ENV并将其设置为development

1.1。 是否build议宁愿切换该命令,并将开发设置作为默认设置,而是使用docker-compose.prod.yml进行覆盖?

1.2。 你如何处理node_modules目录?

我真的不确定如何在本地开发需求之间处理node_modules目录,然后运行Production。 (也许我有一个基本的误解?)


编辑:

我添加了一个.dockerignore文件并将node_modules目录包含在一行中。 这可以确保在复制过程中忽略node_modules dir等。

然后,我编辑了docker-compose.yml ,将node_modules作为一个卷包含在内。

 volumes: - ./:/home/app/code - /home/app/code/node_modules 

为了完整性,我还在问题开始时将上述更改放入了完整docker-compose.yml中。

这甚至是一个解决scheme?

做到以上确保我可以有我的本地发展npm安装包括开发依赖。 当运行docker-compose时,它将引入Docker容器中的仅限生产节点模块(因为默认docker-compose.yml被设置为NODE_ENV = production)。

但似乎在运行docker-compose -f docker-compose.yml build时没有考虑到2 docker-compose文件中设置docker-compose -f docker-compose.yml build :/我期望它发送NODE_ENV = production,但是所有的node_modules都被重新设置,安装(包括dev-dependencies)。

我们是否使用2个Dockerfiles? (Prod的Dockerfile;本地开发的Dockerfile.dev)

(我觉得这是我在设置中缺less的基本逻辑/知识)


2. Nodemon vs PM2

如何在本地开发机器上使用nodemon ,而在生产版本上使用PM2?

3.是否应该在Docker容器中创build一个用户,然后将该用户设置为在Dockerfile中使用?

它默认使用root用户,但是我还没有看到许多文章谈论在容器中创build一个专用的用户。 我正确的做了什么,我已经做了安全吗? 在非Docker构build中,作为root运行应用程序当然不会感到舒服。

感谢您的阅读。 任何和所有的帮助赞赏:)

    1. 要么没有太大的关系,我更喜欢有发展的细节,然后覆盖生产的细节。

    2. 我不提交给我的回购,然后我在我的dockerfile“npm安装”。

  1. 您可以根据构build设置在dockerfile中设置规则。

  2. 通常是通过root来构build所有东西,并通过root运行主程序。 您可以设置其他用户,但对于大多数用途而言,并不需要,因为Docker容器的想法是隔离各个泊坞窗容器中的每个进程。