This case is based on CentOS 7 system

Suitable for people with some experience in using Docker

Suitable for those who have some experience in using Linux commands

1. Docker part

1.1 Introduction to Docker

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and then distribute them to any popular Linux machine, as well as virtualization. Containers are completely sandboxed and have no interface with each other

1.2 docker architecture

In short, Docker is a lightweight Linux system. Docker containers are created by Docker images. The relationship between containers and images is similar to that between objects and classes in object-oriented programming. The Docker architecture is shown below:

1.3 Docker VM management commands

1.4. Install Docker

  1. Update software library
    yum update -y
Copy the code
  1. Install the docker
    yum install docker -y
Copy the code

1.5. Start docker service

  1. Start the Docker service
    service docker start
Copy the code
  1. Other related commands
Service docker restart // Restart the docker service service docker stop // Stop the docker serviceCopy the code

2. Node

You can write a hello-world project using the KOA framework and create a new Dockerfile in the project’s root directory

Dockerfile is a text file containing instructions, each Instruction builds a layer, so the content of each Instruction describes how the layer should be built.

The author’s own Dockerfile configuration information is as follows, people familiar with Docker can configure themselves

# Dockerfile
# use Node as the mirror
FROM node
Create the directory in the container
RUN mkdir -p /home/project
Set the working directory of the container to this directory
WORKDIR /home/project 
Provide 3000 ports externally
EXPOSE 3000
The command to execute after the container is created
CMD npm install --registry=https://registry.npm.taobao.org && node ./start.js
Copy the code

Post the project on Github in preparation for Jenkins deployment

3. Jenkins

  • Query Jenkins mirror
    docker search jenkins 
Copy the code

  • Pull the latest Jenkins image
    docker pull jenkins:latest
Copy the code
  • Start the Jenkins
	sudo docker run -d -u 0 --privileged  --name jenkins_node1 -p 49003:8080 -v /root/jenkins_node1:/var/jenkins_home jenkins:latest
Copy the code
  • Command parsing:
  1. -u 0

-v /root/jenkins_node1:/var/jenkins_home. 2

Map the directory /var/jenkins_home in the docker container to /root/jenkins_node1

  1. –name jenkins_node1

Name the container Jenkins_node1

  1. -p 49003:8080

Port mapping: Maps port 8080 of the container to port 49003 of the host

  1. –privileged

Grant maximum permission

  • The meaning of the entire command

Run jenkins_node1 with Jenkins :latest as the latest container, overwrite the accounts in the container with root account, give the highest permissions, map /var/jenkins_home to /root/jenkins_node1 on the host. Map port 8080 in the container to port 49003 on the host

  • Check the Jenkins

After execution is complete, wait for a few tens of seconds for the Jenkins container to start initialization.

You can check whether there are too many files under the host /root/jenkins_node1

Go to your browser and type localhost:49003 to check whether Jenkins has started successfully

If the following information is displayed, the startup is successful:

  • To get the password
cat /root/jenkins_node1/secrets/initialAdminPassword
Copy the code

Copy the output password, paste it into the page, and click continue to take you to the page below

Click on the install

After the installation is complete, the page for creating an administrator account is displayed

Enter the account password information and click Save (information to be filled in) to go to the home page

Configure Jenkins and enter the system management page to manage the plug-in

Select the SSH plug-in,

Install directly, wait until the installation is complete, return to the home page,

Go to System Administration -> System Configuration

Drag it down to Publish over SSH

Select Advanced, enter server IP, user name, and password, and then click Test Configuration

If Success is displayed, the configuration is correct. Then save and return to the home page

Create a new project

Enter the project name

Choose source management, use Git management, enter github repository address, add Github user

Complete to select the build environment,

Command executed

sudo docker stop nodeapp || true \
 && sudo docker rm nodeapp || true \
 && cd /root/jenkins_node1/workspace/node  \
 && sudo docker build --rm --no-cache=true  -t node  - < Dockerfile \
 && sudo docker run -d --privileged=true --name nodeapp -p 3000:3000 -v /root/jenkins_node1/workspace/node:/home/project node
Copy the code

After saving, click Build Now

After the build is successful, you can see your project files under the host directory /root/jenkins_node1/workspace/node

Enter the docker server address localhost:3000 in the browser to access the page information

If the startup fails, you can view logs to determine the cause of the failure

docker logs nodeapp
Copy the code

4. Jenkins + Github automatic deployment

If you want the local code to be pushed to Github and Jenkins to automatically pull up the latest code for redeployment, read on

The server must be accessible from the Internet. If you want to test the local environment, try penetrating natApp or Ngrok from the Intranet

  1. Click on the user on the home page

Home -> User -> root

  1. Go to Settings -> Show API Token

  1. Copy the value inside the API Token

  2. Go back to the home page -> Node -> Configure -> Build trigger paste into the authentication token

  1. Log in to your Github project and go to Setting -> Webhooks -> Add Webhooks

  1. Add webhooks

  1. Modify Jenkins’ security policy

On the Jenkins home page, go to System Administration –>Configure Global Security

At this point, after git push is complete, Jenkins automatically builds automatically deploy.

5, Docker common command

Those who are more interested in Docker can take a look and continue to learn

Image correlation

  • The query image
docker search [name]
Copy the code
  • Pull the mirror
docker pull [name]
Copy the code
  • Import mirror
docker load < /home/node.tar.gz
Copy the code
  • Export the mirror
docker save > /home/node.tar.gz
Copy the code
  • Querying All Mirrors
docker images
Copy the code
  • Remove the mirror
docker rmi [name]
Copy the code
  • Changing the Name of an Image
docker tag docker.io/node node
Copy the code

The container related

  • Start the
Run and enter interactive mode
docker run -it --name myjava java bash 
# run in the background
docker run -d --name myjava java
Copy the code
  • Port mapping
docker run -it --name myjava -p 9000:8085 -p 9000:8086 java bash
Copy the code
  • Directory mapping
docker run -it --name myjava -v /home/project:/soft --privileged docker.io/node bash
Copy the code
  • Enter the container running in the background
docker exec -it name bash
Copy the code
  • Automatic restart
docker  run --restart=always -it --name myjava -p 9000:8085 -p 9000:8086 java bash
Copy the code
  • Suspension of the container
docker pause node
Copy the code
  • Stop pause container
docker unpause node
Copy the code
  • Stop the container
docker stop node 
Copy the code
  • Start the container
docker start -i node
Copy the code
  • Check the container
docker ps -a
Copy the code

Docker network segment dependent

  • Create a network segment
docker network create net1
Copy the code
  • View network segment information
docker network inspect net1
Copy the code
  • Example Delete network segment information
docker network rm net1
Copy the code

6, summary

I recently learned node, so I want to try to achieve this automatic deployment process through Docker+ Jenkins, I stepped on a sinkhole, finally is a successful configuration. Pm2 is used to manage node projects, and pM2 is used to automate node project deployment. Deploy node projects automatically using PM2. Kind of a summary note for myself. Please point out any bad points.

The public,

Welcome to follow my public account “code development”, share the latest technical information every day. Focus on getting the latest resources