Jenkins + docker deploying nodejs program

Time:2021-1-10

Recently, Jenkins + docker was used to complete the continuous deployment of nodejs program. The front line is very long, the knowledge points are very fragmented, and some pits have been stepped on. So I wrote an article to summarize it. If you want to forget it in the future, you can also come back to read it.

First of all, let’s talk about the goal: after the user submits the code and tags it, select the corresponding tag on Jenkins interface to execute the build. During the build process, the code will be packaged into an image through docker, and then pushed to the image warehouse. Then SSH it to the application server to pull the corresponding image, stop the old version container that was running before, and run the new image.

The flow chart is simple
Jenkins + docker deploying nodejs program
After understanding the goal, start to work step by step.

First, install the docker environment. My machine is Ubuntu 16.04

Add software warehouse (Alibaba cloud warehouse)

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
     "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
     $(lsb_release -cs) \
     stable"

Install docker

sudo apt-get update
sudo apt-get install docker-ce

Restart docker

sudo service docker restart

Test whether the docker is installed successfully. If you print out the corresponding version of docker by executing the following command, it means that the docker has been installed successfully

docker --version

After the docker is successfully installed, Jenkins will be installed based on this docker environment

Due to the problems of the domestic website, you can add the accelerator of daocloud first: find the accelerator option on the official website of daocloud and find your corresponding operating system

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://dfe09f6e.m.daocloud.io

Pull Jenkins

sudo docker pull jenkins:lts

Create Jenkins configuration file directory

sudo mkdir /var/jenkins_home
sudo chmod 777 /var/jenkins_ Note that the permissions of the local volume must be configured here, otherwise the startup fails

Running Jenkins image

Note: we build the image through Jenkins, but there is no docker installed in our Jenkins container, but our host computer has installed docker, so we need to hang the docker of the host computer to the container. Refer to the link below: https://www.jianshu.com/p/8b7…

sudo docker run -d \ 
   -p  9000:8080 \ 
   -v  /var/jenkins_home:/var/jenkins_home \  
   --restart=always \
   #After the docker is restarted, the container will also be restarted  
   -u root \ 
   -v  /usr/bin/docker:/usr/bin/docker \ 
   -v  /var/run/docker.sock:/var/run/docker.sock \  
   -v  /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 \  
   --name myjenkins jenkins:lts

-d: Background operation

-p: The 9000 port of the host is mapped to the default 8080 port of Jenkins

-v: Mount the host’s directory into the container. After the container stops, Jenkins’s configuration still exists

Name: give the container a name

At this time, you can see the Jenkins interface by visiting port 9000. Note: if it is a server on alicloud, you need to configure the allowed port 9000 in the security policy

After visiting the Jenkins interface, you will be prompted to enter a password, find the corresponding file according to the prompt, view the password, and copy it

cat  /var/jenkins_home/secrets/initialAdminPassword

Then install the plug-in according to the prompts
Jenkins + docker deploying nodejs program
Wait for plug-in installation to complete

After the plug-in is installed, the user is created, and then the initialization is completed.

Next, install the plug-ins we need to deploy nodejs. Our goal is to select the tag on git on Jenkins as the source code, so we need to install the GIT parameters plug-in

Click system management – plug in management – select the optional plug-in tab, find the GIT parameter plug in installation, then search SSH to find the SSH plugin installation, and wait for the installation to complete

After the plug-in is installed, our basic work is basically done. Next, we will start to create tasks on Jenkins to achieve our goal. Click new task, fill in a task name, select “build a free style software project”, and then select parameterized construction process to select tag for construction
Jenkins + docker deploying nodejs program

Next, fill in the GIT warehouse information
Jenkins + docker deploying nodejs program

Fill in the specified branch${Tag}Then, in the build script, you can get the tag selected during the build through $tag

Then there’s configurationstructureNow, choose to execute the shell

#Enter project directory
cd  /var/jenkins_ Home / workspace / [fill in your git warehouse name]
#Tag name
tagName=$Tag
#Docker warehouse. You can apply for your own image warehouse in alicloud image warehouse
Repository = [domain name of image warehouse] / [namespace of image warehouse] / [name of image warehouse]: Jenkins - $tagName
#To build an image, pay attention to the following one. It represents to use the dockerfile under the current target. Later, it will say how to write the dockerfile
docker build -t $REPOSITORY .
#If it's a private image warehouse, such as my alicloud image warehouse, you need to log in to push. If it's public, you don't need this step
Docker login - u [user name] - P [password] [image warehouse domain name]
#Push the constructed image to the remote image warehouse
docker push $REPOSITORY
#Delete locally built images
docker rmi -f $REPOSITORY

As mentioned above, we need to have a dockerfile to build an image. This dockerfile is actually a docker. When building an image, we package the image according to the contents of this file. Take a look at the dockerfile of my project, which can be used for reference. It’s very simple

#To build a new image based on a certain image, we are a nodejs project. We choose an image with nodejs environment
FROM node:13.5.0-buster-slim
#Put a label on it,
Label Zhangchao [email name]
#Set the environment variable. Nodejs code can get the environment variable. If there is no environment variable, you can not write it
ENV NODE_ENV=production
#Create a code directory
RUN mkdir -p /usr/src/webapp
#Copy the code to this directory
COPY . /usr/src/webapp
#Set working directory
WORKDIR /usr/src/webapp
#Installing nodejs project dependencies
RUN /bin/sh -c 'cd /usr/src/webapp/ && \                
                           npm install --unsafe-perm'
#Exposed 3000 ports
EXPOSE 3000
#Start nodejs service
CMD node server.js

After packaging the image, we will take the last step, which is to shell it to the application server through the SSH plug-in of Jenkins we just installed, stop the image of the previous version, and run our newly built image

Let’s first configure the address information of our application server.

Add a domain, where the domain name can be written freely
Jenkins + docker deploying nodejs program

Add a certificate under the domain created in the previous step, and select the type of user name and password under user name with password, that is, the login account and password of the Linux server to be connected
Jenkins + docker deploying nodejs program

Add remote host

Enter Jenkins – > system management – > system configuration – > SSH remote hosts (find SSH remote hosts in system settings) and add the Linux server connection
Jenkins + docker deploying nodejs program

After the configuration is completed, we continue to the newly created task and add the build steps
Jenkins + docker deploying nodejs program

Select the configured SSH site and fill in the script executed remotely

#Set tagName
tagName=$Tag
#Log in to the private warehouse. If it is a public warehouse, you don't need to write this step
Docker login - u [user name] - P [password] [image warehouse domain name]
#Pull the remote image pull is followed by the warehouse Name: tag name
Docker pull: Jenkins - $tagName
#Find container ID by container name
Containerid = ` docker PS - a | grep [container name launched in previous version] | awk '{print $1}'`
#Find the image name based on the container name
Imagename = ` docker PS - a | grep [container name launched in previous version] | awk '{print $2}'`
#If the container ID exists, stop running the old container and delete the old container and image
if [ -n "$containerId" ]; then  
  docker stop $containerId 
  docker rm $containerId  
  docker rmi -f $imageName
fi
#To run a new image, the last face is actually the warehouse Name: tag name
Docker run - D -- name [container name] - P 3000:3000 [domain name of image warehouse] / [namespace of image warehouse] / [name of image warehouse]: Jenkins - $tagName

Click save, and our build process is configured. You can click build with params and select the tag of GIT warehouse to build. If you have any questions, you can put forward them in the comments. I will reply when you see them. I hope you can succeed once.

Reference link:
Ubuntu 16.04 installation and configuration docker:
https://www.jianshu.com/p/724…
How to execute the docker command of the host from inside the container:
https://www.jianshu.com/p/8b7…
Remote SSH using Jenkins plug-in SSH plugin
https://blog.csdn.net/df0128/…

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]