Implementation of centos7 + docker + Jenkins + core 2.0 automatic release and deployment



Docker has always been very hot, and has always wanted to move the original Jenkins automatic deployment tool to docker. But this year, it has been busy with various things, and it has not implemented this thing. It just ushered in the official release of dotnet core 2.0. While upgrading the project, Jenkins was directly moved to Docker by the way. Why write this article? Because I have looked for relevant materials, most of the articles are based on Ubuntu installation. Net core or gitlab for continuous integration and automatic deployment, etc., no one has tried to deploy Jenkins on CentOS 7.3 and build core 2.0 applications. Of course, I don’t know why Ubuntu is so popular.

Next, the author will roughly teach you how to install docker and Jenkins and use Visual Studio 2017 to create a new demo and realize automatic deployment from this blog post. I won’t say much about it. Let’s take a look at the detailed introduction.

Environmental preparation

Hardware configuration: 1-core 1g1m bandwidth. 50G hard disk.

System environment: CentOS 7.3 (64 bit)

In fact, Tencent cloud is used

Install Docker

In fact, during the installation of docker, you can go to the official website of docker to find the corresponding installation documents for installation. Docker distinguishes between CE and EE versions. What’s the difference between the two versions? You can refer to the relevant materials by yourself, which will not be summarized here. If CE is used for the tutorial, then find the corresponding official document to install. Here are the steps for the tutorial installation

1. Update the bag, it’s more harmless

# sudo yum -y update

2. Install the required software package (in fact, Tencent cloud has all these packages, but it is afraid that some castration systems are missing, so paste the command, and the installed command will not cause damage if used)

# sudo yum install -y yum-utils \
 device-mapper-persistent-data \

3. Use stable version of the warehouse (of course, you can use the latest version or test version of the warehouse, which needs official instructions)

# sudo yum-config-manager \
 --add-repo \

4. Install docker CE

# sudo yum install docker-ce

Tips: on production systems, you should install a specific version of docker CE instead of always using the latest version.

5. Start docker

# sudo systemctl start docker

6. Docker verifies that it is installed correctly by running the Hello world image.

# docker run hello-world

If the following interface appears, the installation is successful

Configure docker accelerator

This step is not necessary. If your environment has a ladder or network speed, you can ignore this step.

$ curl -sSL | sh -s

The premise of using is that you must use Tencent cloud, which cannot be accessed outside. Only Tencent cloud can use this image address. You can use the daocloud acceleration address. For details, please refer to the relevant information separately.

Install Jenkins

First, do not directly pull the image file of Jenkins from the docker store, because the docker of dotnet core needs to be automatically deployed in the future, and the docker on the host needs to be directly operated, so you need to mount the docker to Jenkins image, so now you need to write your own dockerfile to build your own Jenkins.

# touch Dockerfile
# vim Dockerfile

Insert the following:

FROM jenkins

USER root
#Clear the source of the basic image settings and switch to the Jessie source of Tencent cloud
#To use non Tencent cloud environment, change tencentyun to aliyun
RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list \
 && echo "deb jessie main contrib non-free" > /etc/apt/sources.list \
 && echo "deb jessie-updates main contrib non-free" >> /etc/apt/sources.list \
 && echo "deb jessie/updates main contrib non-free" >> /etc/apt/sources.list
#Update source and install missing packages
RUN apt-get update && apt-get install -y libltdl7 && apt-get update

ARG dockerGid=999

RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group 

#Install docker compose because of the need to build the environment later
RUN curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

RUN chmod +x /usr/local/bin/docker-compose

If libltdl7 is not installed, the following problem will occur, because there is no such package inside the Jenkins container

docker: error while loading shared libraries: cannot open shared object file: No such file or directory
Build step 'Execute shell' marked build as failure

When Jenkins operates the docker of the host, access is denied. In fact, the Jenkins user of the container does not have access to the docker command outside the container, so the run command needs to add Jenkins user to the docker user group

Building Image

# docker build . -t auto-jenkins

The successful content above indicates that Jenkins is installed successfully

When you start Jenkins, you need to create a Jenkins configuration directory and mount it to the Jenkins directory in docker

# mkdir -p /var/jenkins_home

Running Jenkins

# docker run --name jenkins -p 8080:8080 -p 50000:50000 \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v $(which docker):/bin/docker \
 -v /var/jenkins_home :/var/jenkins_home \
 -d auto-jenkins

Line 1: build a container named Jenkins – P is the container running open port

The second and third lines: – V is to mount the docker file of the host into the container

Line 4: create the configuration directory of the host, mount it in the docker container, and the Jenkins configuration directory file in the container will be mapped out

Line 5: use auto Jenkins image and start in the background

At this time, run Jenkins will prompt success

After the docker PS command is executed, nothing is found

Execute the docker PS – a command to see

Execute docker start – a Jenkins to let the container foreground output running information

It’s not hard to see that Jenkins can be started. It’s just the permission problem that causes this error.

Now I need to take a look at Jenkins’s official dockerfile

I know the cause of the problem and put the code on it.

##Modify / var / Jenkins folder permissions
# chown -R 1000 /var/jenkins_home

Continue to run Jenkins code

# docker run --name jenkins -p 8080:8080 -p 50000:50000 \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v $(which docker):/bin/docker \
 -v /var/jenkins_home:/var/jenkins_home \
 -d auto-jenkins

Then you’ll see the message of distress

The container has been running before, so there will be a container that already exists. Brother, I’ll wipe your tears. Execute the following command to delete the container from the original run

# docker rm -f jenkins

After deleting the container, continue with the run command

# docker run --name jenkins -p 8080:8080 -p 50000:50000 \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v $(which docker):/bin/docker \
 -v /var/jenkins_home:/var/jenkins_home \
 -d auto-jenkins

After a string of characters appears, executing docker PS will find that the container has been started, which is normal. Can I use it now? Don’t worry. There’s a long way to go.

Configure Jenkins

Open the browser and enter the address your IP: 8080 to access it. You will be happy to see this interface. It means that your Jenkins has been installed successfully, but it needs to be configured. Now, let’s teach you how to configure

First, enter the container

# docker exec -it jenkins /bin/bash

View the password in the / var / Jenkins? Home / secrets / initialadminpassword file

# cat /var/jenkins_home/secrets/initialAdminPassword

In fact, you don’t have to enter the container to view the password. You can directly view the content of / var / Jenkins / / secrets / initialadminpassword in the host environment, because the author has previously attached this directory to docker

Copy the output content, paste it into administrator password, enter exit to exit the container, and then proceed to the next step. You will see this interface, and click Install recommended plugins

Wait for initialization to complete

At this stage, I’ll fill it out by chance.

Use vs2017 to create an core 2.0 web application and upload the code to GIT. Here I will not demonstrate the process steps of uploading code.

An address is prepared for you to complete this tutorial:

Create a new free style software project, fill in the name and click OK

Fill in source control

Check poll SCM to set the interval between pulling the codes on GIT. Here, I pull them once every two minutes

Delete workspace before build start

Check abort the build if it’s stuck to set the timeout time of the build. If the time used by the build exceeds the set time, then the build is considered to be a failure. The timeout setting here is 10 minutes

Add an execute shell script and fill in the following script

#Get short version number

GITHASH=`git rev-parse --short HEAD`
echo ---------------Remove-Orphans------------------
docker-compose -f ./docker-compose.yml -f ./docker-compose.override.yml -p dockercompose4255153253317384266 down --rmi local --remove-orphans
echo ------------------Config-----------------------
docker-compose -f ./ -p dockercompose4255153253317384266 config
echo ------------------Build------------------------
docker-compose -f ./ -p dockercompose4255153253317384266 up --build
echo ---------------Publishing...------------------
docker-compose -f "./docker-compose.yml" -f "./docker-compose.override.yml" -p dockercompose4255153253317384266 up -d --build

Save the application and return to the project interface. Click build now to deploy success

Open the browser and input your IP: 8088/

Problems encountered

In fact, the above space is for you to realize the deployment as soon as possible, but it briefly introduces some common mistakes on the way. In fact, there are more than these problems when building the CI environment. For example, the content of dockerfile at the beginning is not the same as that in the tutorial. At first it was like this:

FROM jenkins

USER root
#Clear the source of basic image settings and switch to Alibaba cloud's Jessie source
RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list \
 && echo "deb jessie main contrib non-free" > /etc/apt/sources.list \
 && echo "deb jessie-updates main contrib non-free" >> /etc/apt/sources.list \
 && echo "deb jessie/updates main contrib non-free" >> /etc/apt/sources.list
#Update source and install missing packages
RUN apt-get update && apt-get install -y libltdl7 && apt-get update

ARG dockerGid=999

RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group 

USER jenkins

If you find other tutorials on the Internet to teach you how to install Jenkins, and you use a dockerfile similar to this content, the following two errors will occur when Jenkins builds and compiles:

First: lack of docker compose

This situation, of course, can be solved. First, enter the container, and then find the official installation docker compose document for installation. Jenkins uses Linux to find the corresponding Linux Installation data directly.

Second: Authority

If you install docker compose manually from the container, it will fail to install because the Jenkins user does not have permission to install docker compose

To solve the above problems, of course, you can switch the root user, but the author uses the most stupid and direct way, and does not use Jenkins user directly when building the image, so there is no last sentence user Jenkins in the tutorial. This is done to make the tutorial steps smoother

Tips: it is not recommended to switch to root directly in the formal production environment

Running the Jenkins command starts with this:

# docker run --name jenkins -p 8080:8080 -p 50000:50000 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v $(which docker):/bin/docker \
  -v /var/jenkins:/var/jenkins_home \
  -d auto-jenkins

This will result in finding a directory that does not exist at all during construction

In fact, in the third sentence, changing / var / Jenkins to / var / jenkins_homesolves this problem.

At this moment, if you have ever encountered such a problem, brother has wiped your tears now.

Of course, there are not only these problems here, but also some of them are not illustrated without screenshots.

Written in the end

In fact, the author has Google these tutorials, but nothing, found that many people use Ubuntu, is not to find a case of CentOS. Of course, I don’t know why they like to use Ubuntu. I’m here to share my experience in the implementation process, because I’m afraid that many people will talk about the same mistakes as the author, and it’s a miserable day without help, even if I have made some contributions to the community for so long.

Well, the above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaer.