Use Docker deployment NodeJS + MongoDB project

Docker recent study, with Docker and redeployed cdfang-spider project, the use of docker dramatically reduces deployment really difficult. If you also want to deploy their own projects with Docker, let us look down.

Docker article to talk about the following three aspects:

  • Docker development history.
  • Docker basis.
  • Docker project combat.

Docker history of

Ancient times

A long time ago, released a App application, it should be like this. First, buy a physical server, and then manually install the corresponding operating system, application runtime environment to build App, App deploy applications, the last to be accessed by other people. Seemingly nothing wrong with doing so, but it may cause several problems:

  • Deployment is very slow.
    • Purchase receipt physical servers to take time.
    • Manually install the operating system takes time.
    • App installation environment and the corresponding application will take time.
  • Cost is very high.
    • Physical server is very expensive.
  • Waste of resources.
    • If the project is small, we can not fully utilize the resources this server.
  • Difficult to migration and expansion.
    • If the CPU, memory, hard drive is not enough, you can only add a physical device, but this is capped.
  • It may be limited hardware manufacturers.

Virtual Era

In order to solve the many problems of the physical device, there has been virtual machine. After the virtual machine appears significantly reduces deployment difficulty, in order to deploy an application, create a new virtual machine can be, but also according to the size of the application, the appropriate allocation of system resources.

Virtual technology has the following characteristics:

  • A physical machine resources assigned to a different virtual machine.
  • It is easy to expand, plus physical machine / virtual machine.
  • It is easy to cloud technology, Ali cloud, AWS and so on.

Virtualization technology enables the isolation of the physical layer , but there are the following problems:

  • Each virtual machine is a complete operating system, each new had to manually install it again.
  • Virtual machine environment projects also need to be reinstalled every time.
  • System resources consumed by the virtual machine itself will be more.

Container era

In order to facilitate the deployment of the project, there has been container technology, mainly has the following characteristics:

  • Implement packaged applications and their environment.
  • Achieve mutual isolation between applications, sharing the same operating system kernel.
  • The container itself is relatively light, compared to a virtual machine, take up less system resources.

Docker is a container technology and the most popular one. Docker provides an isolation mechanism, different applications will dependencies library packaged together and running in separate containers in order to achieve the isolation layer is applied .

Two mechanisms container technology is mostly based on the Linux kernel provides: Cgroups (to achieve resource distribution according to need) and Namespace (implementation tasks isolation).

Virtualization vs containerized

Virtualization and containers of all current differences between mainstream deployment of technology, both of which are as follows:

  • Virtual machine technology has evolved over the years, supporting technologies and standards have been standardized, and the container before the rise in recent years, supporting technology and standards are still perfected.
  • Because the GuestOS VM (virtual machine operating system) is present, and can run different host OS, and the container can support the same and host operating system kernel, relatively poor barrier properties.
  • Container significantly more lightweight than the virtual machine on the host operating system, the container just like a similar process. Therefore, the container has a faster boot times, easier cluster management and so on. And because there is no presence of GuestOS, run applications directly and almost no loss of performance on the host in a container, it is superior to the performance of the virtual machine.

Docker basis

The core Docker is to achieve isolation at the application layer Docker Engine layer.

Docker stratified
Application (Application Layer)
Container (container layer)
Docker Engine (isolation layer)
Host OS operating system
infrastructure (infrastructure)

Client and Server Docker is divided into two parts, we execute the command in Docker Client, the last created Container and Image will run on Server. Dcoker architecture as shown below:

Image

Image is mainly used to package applications and dependent on its environment, to provide the necessary environment for the Container and installed applications. Image itself and can not be executed, only to run through the Container.

Image features the following main points:

  • A collection of files and meta data (root filesystem).
  • Layered, each layer can be added and deleted files change, to become a new Image.
  • Different Image can share the same layer.
  • Image itself is read-only.

Image to be constructed by Dockerfile, may be pulled up by DockerHub.

Dockerfile is a text file that contains all the commands to build the Image. Docker can docker buildbe constructed automatically read command from the Dockerfile Image. Common configuration information can be found below in the comments Dockerfile file, also I suggest that you read the official document Dockerfile Reference .

Container

Container is an example of running Image by docker run imageto start and run a Container.

Container mainly the following characteristics:

  • Created by Image.
  • Establishing a layer on top of Image Container (read-write).
  • Object-oriented analogy: Class (Image) and Example (Container).
  • Image App responsible for storage and distribution, Container responsible for running App.

Networks

Use Dcoker deployment project often generates a lot of containers, which can only be accessed via the default ip address, but a new container produced by ip address is not controllable, which has brought some trouble to the communication between the vessel. Network Docker use to manage the communication between the container, as long as the two Conteiner is in the same Network, can communicate with each other through the vessel to the name.

Docker built 5 types of Network:

  • bridge (bridge in the same container can be accessed from each other).
  • Host (with the container through the host computer connected to a network, although straightforward, but the isolation container cracked).
  • none Disable all networks.
  • overlay cluster use.
  • macvlan.

In addition to these 5 Network, users can also custom-written Network Plugin.

Docker Compose

Docker Compose is a tool, which can be defined Docker container through a multi yml file. You can go to create or manage multiple containers according to the definition yml files with a single command. Next are using the command line and Docker Compose way to compare the way to create the container.

Do not use Docker Compose create container

docker pull yhlben/cdfang-spider
docker pull mongo
docker network create webapp-network
docker run -d --network webapp-network -v ~/data/db:/data/db mongo
docker run -p 8082:8082 --network webapp-network -d yhlben/cdfang-spider
复制代码

Visible, manually create the container, you need to manually execute many commands on the command line, which once had the wrong order, you have to again, not easy to manage container.

Use Docker Compose create container

1, the new docker-compose.yml file.

version: '3.7'
services:
  database:
    image: mongo
    restart: always
    volumes:
      - ~/data/db:/data/db
    networks:
      - webapp-network
  web:
    image: yhlben/cdfang-spider
    depends_on:
      - database
    ports:
      - 8082:8082
    networks:
      - webapp-network
networks:
  webapp-network:
    driver: bridge
复制代码

2, run docker-compose

docker-compose up -d
复制代码

Visible, use Docker Compose to create a container only need to write well yml file in advance, then execute a command on the line, compared to manually type commands, more convenient.

In addition, Docker Compose also possible to use docker-compose -scaleextended compatible plurality of containers, for load balancing, expansion may be, may be reduced volume. For example: seamless deployment project, the first expansion of a new Container, Container when the boot is completed, join the cluster, and then update the old container, and then finished updating join the cluster.

Docker Compose configuration

Docker Compose profile general definition docker-compose.ymlfile, the main configuration items as follows:

  • services
    • A service on behalf of a container, the container can be created from dockerHub in the mirror, you can also use the local dockerfile build out to create a mirror image.
    • start a similar service docker run, you can specify the network service and the volume of reference.
  • networks
    • Defined networks, equivalent to execution docker network create xxxx.
  • volumes
    • Defined volume, equivalent to execution docker volume create xxx.

More configuration item can refer to the official documentation compose-file

Docker project combat

Next to cdfang-spider project, for example, the use of Docker deployment project.

Full manual deployment

1, write Dockerfile file.

# 加载基础镜像
FROM mhart/alpine-node

# 注释
LABEL maintainer = "yhlben <[email protected]>"

# 创建工作目录
RUN rm -rf /app
RUN mkdir /app
WORKDIR /app

# 安装项目依赖
COPY . /app
RUN npm install
RUN npm run build
RUN mv ./dist/* ./

# 对外暴露端口
EXPOSE 8082

# 启动 Image 时执行命令
CMD BUILD_ENV=docker node app.js
复制代码

2, constructed by Dockerfile file Image.

docker build -t yhlben/cdfang-spider .
复制代码

3, pull mongo Official Image.

docker pull mongo
复制代码

4, create a network, so that two containers can communicate with each other.

docker network create webapp-network
复制代码

5, run container

docker run -d --network webapp-network -v ~/data/db:/data/db mongo
docker run -p 8082:8082 --network webapp-network -d yhlben/cdfang-spider
复制代码

6, by accessing localhost: 8082 to access the project.

Automated Deployment

1, write Dockerfile file.

# 加载基础镜像
FROM mhart/alpine-node

# 注释
LABEL maintainer = "yhlben <[email protected]>"

# 创建工作目录
RUN rm -rf /app
RUN mkdir /app
WORKDIR /app

# 安装项目依赖
COPY . /app
RUN npm install
RUN npm run build
RUN mv ./dist/* ./

# 对外暴露端口
EXPOSE 8082

# 启动 Image 时执行命令
CMD BUILD_ENV=docker node app.js
复制代码

2, authorized the project on dockerHub github, github project so that when an update is performed automatically Dockerfile build, build and save the results to dockerHub warehouse.

3, write docker-compose.yml file.

version: '3.7'
services:
  database:
    image: mongo
    restart: always
    volumes:
      - ~/data/db:/data/db
    networks:
      - webapp-network
  web:
    image: yhlben/cdfang-spider
    depends_on:
      - database
    ports:
      - 8082:8082
    networks:
      - webapp-network
networks:
  webapp-network:
    driver: bridge
复制代码

4, a key to start, make sure docker-compose installed.

docker-compose up -d
复制代码

5, by accessing localhost: 8082 to access the project.

to sum up

After the completion of the deployment project by Docker, feel very good, mainly divided into the following points:

  • Use Docker Compose a key to start the project.
  • No longer installed on the server to all sorts of environments, all packaged to Image, the Container starts a run up on the line, when not delete Container on the line, will not be any contamination on the server.
  • For the time-consuming Image build process, go directly to dockerHub automated build.

Finally, attach the project Source Address: cdfang-Spider

This project uses a stand-alone deployment, that all containers are on the same server. In addition, docker container supports distributed deployment, you can use docker swarm or kubernetes to manage and is still learning, for the early collated share to everyone, thank you for your support!

Reproduced in: https: //juejin.im/post/5d0737a86fb9a07f087095f2

Guess you like

Origin blog.csdn.net/weixin_33835690/article/details/93172993