docker是一个开源的应用容器引擎,能够为咱们提供安全、可移植、可重复的自动化部署的方式。docker采用虚拟化的技术来虚拟化出应用程序的运行环境。如上图同样。docker就像一艘轮船。而轮船上面的每一个小箱子能够当作咱们须要部署的一个个应用。使用docker能够充分利用服务器的系统资源,简化了自动化部署和运维的繁琐流程,减小不少由于开发环境中和生产环境中的不一样引起的异常问题。从而提升生产力。html
docker三个核心概念以下:node
镜像(images):一个只读的模板,能够理解为应用程序的运行环境,包含了程序运行所依赖的环境和基本配置。至关于上图中的每一个小箱子里面装的东西。linux
仓库(repository):一个用于存放镜像文件的仓库。能够看作和gitlab同样。git
容器(container):一个运行应用程序的虚拟容器,他和镜像最大的区别在于容器的最上面那一层是可读可写的。 至关于上图中的每一个小箱子里。docker
本文主要是教你们了解如何在Docker容器中设置Node JS:express
有一个可运行工做的NodeJS应用程序npm
经过确保进程在出错时不退出,使节点应用程序具备弹性浏览器
经过在代码更改时自动从新启动服务器,使Node应用程序易于使用安全
利用Docker:bash
快速设置与生产相同的开发环境。
轻松地可以在本地和服务器上切换节点版本
Docker的全部其余 好处
先决条件
Docker已经安装好了
至少入门级节点知识和NPM
1.获取一个简单的Node应用程序
咱们将使用Express,由于它的设置是容易的。
在一个干净的目录中,让咱们从初始化NPM开始,继续运行此命令并按照提示进行操做:
npm init
安装Express:
npm install --save-prod express
编制代码src/index.js
<b>const</b> express = require('express') <b>const</b> app = express() <b>const</b> port = 3000 app.get('/', (req, res) => res.send('Hello World!')) app.listen(port, () => {console.log(`Example app listening on port ${port}!`))
启动一个侦听端口3000并使用Hello World响应的"/"这个URL路由。
2.设置Docker以运行咱们的Node应用程序
咱们将使用docker-compose.yml文件来启动和中止咱们的Docker容器,而不是键入长长的Docker命令。您能够将此文件视为多个Docker容器的配置文件。
docker-compose.yml:
version: "3" services: app: container_name: app # How the container will appear when listing containers from the CLI image: node:10 # The <container-name>:<tag-version> of the container, in this case the tag version aligns with the version of node user: node # The user to run as in the container working_dir: "/app" # Where to container will assume it should run commands and where you will start out if you go inside the container networks: - app # Networking can get complex, but for all intents and purposes just know that containers on the same network can speak to each other ports: - "3000:3000" # <host-port>:<container-port> to listen to, so anything running on port 3000 of the container will map to port 3000 on our localhost volumes: - ./:/app # <host-directory>:<container-directory> this says map the current directory from your system to the /app directory in the docker container command: "node src/index.js" # The command docker will execute when starting the container, this command is not allowed to exit, if it does your container will stop networks: app:
让咱们用这个命令启动docker容器。在后台运行(-d)
docker-compose up -d
在浏览器中访问http://localhost:3000并看到 Hello World!
3. 使得应用变得弹性
若是您以前使用过Node,那么您可能知道若是应用程序中发生错误(如未捕获的异常),那么它将关闭该Node进程。这对咱们来讲真的是个坏消息,由于咱们的代码中确定会有一个错误,而且没法保证咱们的代码100%无错误。此问题的解决方案一般是另外一个监视咱们的Node应用程序并在其退出时从新启动它的过程。有这么多的解决方案,好比linux的supervisord,NPM包永远和PM2等......咱们只须要为本指南选择一个。
将专一于 PM2, 由于我最熟悉它,除了进程管理以外还有一些其余功能,例如文件监视,这将在下一节中派上用场。
安装PM2
npm install --save-prod pm2
PM2能够经过命令行使用,但咱们将设置一个简单的配置文件,就像咱们使用docker-compose.yml文件同样,以防止咱们重复输入长命令
ecosystem.config.js:
const path = require('path') module.exports = { apps: [{ name: 'app', script: 'src/index.js', // Your entry point instances: 1, autorestart: true, // THIS is the important part, this will tell PM2 to restart your app if it falls over max_memory_restart: '1G' }] }
如今咱们应该更改docker-compose.yml文件以使用PM2启动咱们的应用程序,而不是直接从index.js启动它。
docker-compose.yml(仅更改了的选项)
version: "3" services: app: container_name: app # How the container will appear when listing containers from the CLI image: node:10 # The <container-name>:<tag-version> of the container, in this case the tag version aligns with the version of node user: node # The user to run as in the container working_dir: "/app" # Where to container will assume it should run commands and where you will start out if you go inside the container networks: - app # Networking can get complex, but for all intents and purposes just know that containers on the same network can speak to each other ports: - "3000:3000" # <host-port>:<container-port> to listen to, so anything running on port 3000 of the container will map to port 3000 on our localhost volumes: - ./:/app # <host-directory>:<container-directory> this says map the current directory from your system to the /app directory in the docker container command: "npx pm2 start ecosystem.config.js --no-daemon" # The command docker will execute when starting the container, this command is not allowed to exit, if it does your container will stop networks: app:
更改docker-compose.yml文件不会影响已经运行的容器。为了进行更改,您应该从新启动容器:
docker-compose restart
4.使咱们的应用程序易于开发
您可能已经注意到,一旦Node进程启动,那么在从新启动Node进程以前,更改代码实际上并无作任何事情,对于咱们而言,每次都会涉及从新启动Docker容器以激活咱们作出的改变。若是咱们在进行代码更改时自动为咱们从新启动Node进程,那将是理想的选择。
在过去,我已经完成了诸如引入文件监视实用程序和使用该文件监视实用程序来从新启动Docker进行文件更改之类的操做,或者我会使用Nodemon可是在使用Docker时会有一些警告。
最近,当文件发生变化时,我一直在使用PM2来从新启动个人Node进程,并且因为咱们已经从上一步中获取了它,所以咱们没必要安装另外一个依赖项。
ecosystem.config.js(仅添加了watch选项):
const path = require('path')
module.exports = { apps: [{ name: 'app', script: 'src/index.js', instances: 1, autorestart: true, watch: process.env.NODE_ENV !== 'production' ? path.resolve(__dirname, 'src') : false, max_memory_restart: '1G' }] }
若是咱们没有将NODE_ENV环境变量设置为production,则上面的配置文件如今将监视src目录。您能够经过更改index.js文件来测试它,除了Hello World以外还能够将其余内容打印到浏览器中!。在此以前,您须要从新启动Docker容器,由于您更改了PM2运行容器的方式:
docker-compose restart
从新启动Node进程可能须要一秒钟才能完成,若是你想观察它什么时候完成,你能够看到你的Docker日志告诉PM2什么时候完成重启你的Node Process:
docker-compose logs -f
总结
咱们的目标之一是可以轻松更改Node版本,您能够经过更改docker-compose.yml文件中的image选项来完成此操做。
本地安装依赖项是使用本地NPM和Node版本完成的,若是您的本地版本与Dockers不一样,有时可能会致使冲突。使用相同的Docker容器来安装依赖项更安全。您可使用此命令来使用该容器来安装依赖项,而后将其删除
docker run --rm -i -v <absolute-path-to-your-project-locally>:/app -w /app node:10 npm install
如上所述,具备与Docker运行的Node不一样的本地版本多是有问题的。最好在容器内部运行命令以保持一致性。你能够进入一个容器
docker exec -it app bash
上面的命令将把你放到容器中,这样你就能够继续从里面运行命令,即npm run start或npm run test
若是您不想进入容器内部,能够运行这样的命令
docker exec -t app bash -c "npm run start"
参考文章: