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

nod*_*oob 7 node.js docker

我正在尝试将Node应用程序转换为使用Docker但遇到一些我无法回答的问题/问题.

但为了简单起见,我已经包含了一些非常基本的示例文件来保持目标问题.实际上,下面的示例仅链接到Mongo容器,但不在代码中使用它以使其更简单.

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

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"]
Run Code Online (Sandbox Code Playgroud)

泊坞窗,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"
Run Code Online (Sandbox Code Playgroud)

泊坞窗,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
Run Code Online (Sandbox Code Playgroud)

的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

1.如何处理不同的NODE_ENV(开发,生产,升级)?

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

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

  • docker-compose.yml将默认值包括NODE_ENV设置为 production
  • docker-compose.dev.yml重写NODE_ENV并将其设置为 development

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

1.2. 你如何处理node_modules目录?

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


编辑:

我添加了一个.dockerignore文件,并将node_modules目录作为一行包含在内.这可确保在复制期间忽略node_modules目录等.

然后我编辑了docker-compose.yml将node_modules包含为卷.

volumes:
  - ./:/home/app/code
  - /home/app/code/node_modules
Run Code Online (Sandbox Code Playgroud)

我还在问题开始时将上述变化全部docker-compose.yml用于完整性.

这甚至是一个解决方案吗?

这样做可以确保我的本地开发npm install包含dev-dependencies.当运行docker-compose时,它会拉入Docker容器内的仅生产节点模块(因为默认的docker-compose.yml设置为NODE_ENV = production).

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

我们改为使用2个Dockerfiles吗?(用于Prod的Dockerfile;用于本地开发的Dockerfile.dev)

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


2. Nodemon与PM2

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

3.您是否应该在docker容器中创建用户,然后将该用户设置为在Dockerfile中使用?

root默认使用用户,但我没有看到很多文章谈论在容器中创建一个专用用户.我的安全性是否正确?在非Docker构建中以root身份运行应用程序当然不会感到很舒服.

谢谢你的阅读.任何和所有的帮助赞赏:)

小智 0

    1. 要么,这并不重要,我更喜欢有开发细节,然后用生产细节覆盖。

    2. 我不将它们提交到我的存储库,然后我的 dockerfile 中有“npm install”。

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

  2. 通常通过 root 构建所有内容,并通过 root 运行主程序。您可以设置其他用户,但对于大多数用途而言,不需要它,因为 docker 容器的思想是将每个进程隔离在各个 docker 容器中。