I am also preparing to write a background and interface for the Flutter project, which will be deployed online first. So I took some time to look at Jenkins and Docker’s continuous deployment. This technology is also being used by many companies. Thank you for giving me a star

Bright spot

  • Customized shell scripts facilitate the control of the integrated deployment environment
  • If the server is replaced, the redeployment task is small

The preparatory work

First we have to have a server. I use ali Cloud ECS here, east China node. Specific purchase operation baidu will have a tutorial. You will have a public IP address and a password to log in to the server over SSH.

As for Docker, if you don’t understand it, you can go to the document first. Docker containers are like containers on ships. Each container holds its own cargo and does not affect each other. For example, a Redis service, a Mongodb service, can be placed in a separate Container. These containers, in turn, depend on an execution environment. This execution environment is what Docker calls an Image. Each Container manages its own lifecycle.

Jenkins is an open source software project developed in Java for continuous integration. After the script is written in advance and successfully debugged, the script stored last time is automatically executed when the next deployment is required without modification.

Install the Docker

Reference documentation

Uninstall the previous version

$ sudo apt-get remove docker docker-engine docker.io
Copy the code

Install with APT

Since APT sources use HTTPS to ensure that the software download process is not tampered with. Therefore, we first need to add the software package that uses HTTPS transport as well as the CA certificate.

$ sudo apt-get update
Copy the code
$ sudo apt-get install 
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
Copy the code

In view of domestic network problems, it is strongly recommended to use the domestic source, please check the official source in the notes.

To verify the validity of the downloaded software package, add the GPG key of the software source.

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# the official source
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Copy the code

Then, we need to add the Docker software source to source.list

$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

# the official source
# $ sudo add-apt-repository \
    # "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    # $(lsb_release -cs) \
    # stable"
Copy the code

This command adds the stable version of Docker CE APT image source. If you need to test or build Docker CE daily, change stable to test or nightly.

Install the Docker CE

Update apt package cache and install docker-CE:

$ sudo apt-get update

$ sudo apt-get install docker-ce
Copy the code

Automatic installation using scripts

In test or development environment, Docker official provides a set of convenient installation scripts to simplify the installation process. Ubuntu system can use this script to install:

$ curl -fsSL get.docker.com -o get-docker.sh

$ sudo sh get-docker.sh --mirror Aliyun
Copy the code

After executing this command, the script will automatically do all the preparatory work and install the Edge version of Docker CE on the system

Start the Docker CE

$ sudo systemctl enable docker

$ sudo systemctl start docker
Copy the code

Create a Docker user group

By default, docker commands communicate with the Docker engine using Unix sockets. Only root users and docker group users can access the Unix socket of the Docker engine. For security reasons, the root user is not directly used on Linux. Therefore, it is better to add the users who need to use Docker to the Docker user group.

Create docker group:

$ sudo groupadd docker
Copy the code

Add current user to docker group:

$ sudo usermod -aG docker $USER
Copy the code

Log out of the terminal and log in again to perform the following tests.

Test whether Docker is installed correctly

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image whichruns the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the  Docker client,which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
Copy the code

If the preceding information is displayed, the installation is successful.

Mirror to accelerate

If it is found that pulling Docker images is very slow in the process of use, you can configure Docker domestic image acceleration.

Ali Cloud image acceleration

Install the docker – compose

Reference documentation

Run this command to download the current stable version of Docker Compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code

Apply executable permissions to binary files

$ sudo chmod +x /usr/local/bin/docker-compose
Copy the code

Check the version

$ docker-compose --version
Copy the code

Install Jenkins

Docker-compose is composed for docker-compose. Docker-compose is composed for docker-compose. Docker-compose is composed for Docker-compose, docker-compose is composed for Docker-compose.

First we need to create a directory agency on the server (I’m just demonstrating)

/home/jenkins
     - docker-compose.yml
     - jenkins-home
Copy the code

Next we’ll write docker-comemage. yml to install Jenkins

version: '3'    Docker-comemage. yml: docker-comemage. yml
services:       # Multiple container collections
  docker_jenkins: 
    user: root  I used root here to avoid some permissions issues
    restart: always Restart mode
    image: jenkins/jenkins:lts  # specify the image to be used by the service. Here I chose LTS (long term support).
    container_name: jenkins # container name
    ports:      # Exposed port definition
      - '8080:8080'
      - '50000:50000'
    volumes:    # Volume mount path
      - /home/jenkins/jenkins_home/:/var/jenkins_home  # This is the jenkins_home directory inside the container where the directory we created initially is mounted
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker     This is so that we can use the docker command inside the container
      - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose Docker-compose: docker-compose: docker-compose
Copy the code

We need to go into the Jenkins directory and execute:

$ docker-compose up -d
Copy the code

This command will help us automatically pull and configure the image

Not surprisingly you can now open your server address http://xxxxxxx: port number to see this screen:

Open the Jenkins directory you created and go to jenkins-home /home/jenkins-home

Enter secrets directory

$ cat initialAdminPassword
Copy the code

Then copy the text inside and fill in the administrator password

Next you need to install two plug-ins

NodeJS Plugin
Publish Over SSH
Copy the code

Then we slide to the bottom

Start creating the project deployment

The official reference

1. First create an nginx.conf file in the project root directory

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen       80;
    server_name  www.lovelp.xin;  # the domain name
    location / {
      root   /app;  # point to directory
      index  index.html;
      try_files $uri $uri/ /index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}}Copy the code

2. Create a Dockerfile file in the project root directory

FROM node
WORKDIR /app

COPY package*.json ./
RUN npm install -g cnpm --registry=https://registry.npm.taobao.org
RUN cnpm install
COPY ./ /app
RUN npm run build:prod

FROM nginx
RUN mkdir /app
COPY --from=0 /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf

Copy the code

3. Create a setup.sh file in the project root directory to help us execute the script

#! /usr/bin/env bash
image_version=`date +%Y%m%d%H%M`;
#Close the shop_admin container
docker stop shop_admin || true;
#Delete the shop_admin container
docker rm shop_admin || true;
#Delete the shop/admin image
docker rmi --force $(docker images | grep shop/admin | awk '{print $3}')
#Build a shop/admin:$image_versionThe mirror
docker build . -t shop/admin:$image_version;
#Viewing the Mirror List
docker images;
#Build a container shop_admin based on the SHOP /admin image
docker run -p 9527:80 -d --name shop_admin shop/admin:$image_version;
#See the log
docker logs shop_admin;
#Delete images generated during build#docker image prune -a -f
docker rmi $(docker images -f "dangling=true" -q)
#Automatically clean up the space
docker system prune -a -f

Copy the code

4. Finally we need to create the project on Jenkins

Finally, we can Build Now happily

Writing is not good so most of the way to use the screenshots I hope you will bear with me, the follow-up will be a node deployment mode, the article is over here, praise to praise O(∩_∩)O ha ha ~