Docker + Jenkins realize automatic deployment

1. Introduction:

This article mainly records how to install jenkins through docker and deploy the project through jenkins. The final effect is that as long as you click build on a project in jenkins, jenkins will go to gitLab to pull the latest code of the latest project, and then based on the pom of your own project. xml file, package the project into a jar, and automatically run the latest jar to achieve the goal of one-click build.

2. Operation

(1). First, you must install these three things on the server: jdk, git, and maven. For installation, please refer to: JDK installation , maven installation , git installation .
(2) Okay, after completing the installation of the above three software, the next step is to get started:
docker   pull   jenkins/jenkins:2.344      #拉取jenkins镜像
#把git的所有东西全部映射
docker run -u root -d -p 10240:8080 \
-p 10241:50000 \
-v /run/docker.sock:/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /var/jenkins_mount:/var/jenkins_home \
-v /usr/local/maven/apache-maven-3.8.5:/usr/local/maven/apache-maven-3.8.5 \
-v /usr/local/git/:/usr/local/git/ \
-v /etc/localtime:/etc/localtime \
-e TZ=Asia/Shanghai \
--name myjenkin #镜像id

To allow dockerthe container to use the gitAND mavencommand, you need to add the AND data volume mapping at Jenkinsstartup.gitmaven

-v  /usr/local/maven/apache-maven-3.8.5:/usr/local/maven/apache-maven-3.8.5

-v /usr/local/git:/usr/local/git #把git的所有东西全部映射

If you need to use the host inside the container, you need to map the data volume using the commands dockerin the host.docker

-v /run/docker.sock:/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \

When git does content volume mapping, it needs to map all the things in git, otherwise an error will be reported.
Insert image description here
After the jenkins container is started, we need to open the port to 10240. How to open the 10240 port?

本人是直接添加的白名单
firewall-cmd --list-ports
firewall-cmd --zone=public --add-port=10241/tcp --permanent
(3) Next, you can witness the miracle: Browser access: your server ip: 10240. Not surprisingly, you will be able to access jenkins

The first thing that catches your eye is this. Jenkins requires you to enter the initial password. Just go to the path prompted in the picture above and copy the initial password: /var/jenkins_home/secrets/initialAdminPassword.
Insert image description here
If Jenkins cannot be accessed, you can execute docker logs -f myjenkins and report an error. Generally, insufficient permissions cause problems with content volume mapping:

touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to/var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

You need to modify the directory permissions because when mapping a local data volume, the owner of the /var/jenkins_home directory is the root user, and the uid of the jenkins user in the container is 1000. Just execute the following command:

chown -R 1000:1000 /var/jenkins_home
docker run -dt-p 10240:8080 -p 10241:50000 --privileged=true -v
#--privileged=true让容器具有root权限,便于进入容器
#如果执行命令之后还是无法启动的话直接 docker run -u root 指定操作用户
#因为Jenkins启动时候的默认用户不是root权限不够
(4) Enter and install in [System Management]->[Plug-in Management] -> [Optional Plug-in]. As shown below:

Insert image description here
Insert image description here
Insert image description here

  • InstallMaven Integration
  • Installation Publish Over SSH(if you do not need remote push, no need to install)
  • If you use Giteecode cloud, install the plug-in Gitee(Git comes with it, no need to install it)

If you can't read English, students can also install a Chinese plug-in: enter chinese to see that the new version of Jenkins comes with a Chinese plug-in, just restart Jenkins a few times.

(5), [System Management] -> [Global Tool Configuration] -> Find Git and Maven

Insert image description here
Insert image description here
Insert image description here
When configuring, you need to note that /usr/local/git/this address is the installation directory of git. /bin/gitThis is the program that executes the git command. We need to find it if we need to execute the git command.

(6) After configuration, if the project needs to be deployed on a remote server: [System Management] -> [System Configuration] -> [Publish over SSH] There are two verification methods, password method and secret key method.

Method 1 : Password verification
Insert image description here
Method 2 : Secret key method
First read centos to generate ssh_kyes to get the public key and private key, then put the private key under the generated id_rsa file and fill in
Insert image description here
the name, host IP, ssh user name, and remote directory (the last Easy to write /, you can use the absolute path later)
Insert image description here
Click [Test Configuration] to test the connection and Success will be displayed;

(7) After completing the previous preparations, you can start creating the project.

Insert image description here
Then click: Git -> Add Repository -> Add (jenkins credentials)
Insert image description here
Insert image description here
After configuring these, we can automatically obtain the code of the remote warehouse

(8) After configuring Git in the previous step, we still need to configure how to package:

Select: Bulid -> Follow the following instructions: Use maven for automatic packaging

clean install -Dmaven.test.skip=true
(9) After everything is configured, you can try to package it: Go to the homepage, find the project you added, and click Build

Insert image description here
After clicking Build, there will be an execution record. Click on the execution record to view the console output. You can find it. Successes appears at the end of the maven packaging directory, which means that the packaging is successful and you can proceed with the process.
Insert image description here

(10) , write compiled operations

Since I Jenkinsneed to deploy it on the same host as the project, I use dockerfilethe method to deploy the project. Here, I need to execute dockerthe command of the host, so JenkinsI need to do data volume mapping when starting, otherwise the container cannot use dockerthe command without specifying The data volume needs to be installed in the containerdocker

#Jenkins启动时加上
-v /run/docker.sock:/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \

Enter the project/var/jenkins_mount/workspace/project directory and create a Dockerfile file

vim Dockerfile

The file content is

FROM lifeng/jdk:11
MAINTAINER dengjiawne <[email protected]>
RUN yum install dejavu-sans-fonts fontconfig -y
# 宿主机文件挂载目录
VOLUME /opt/java_app_docker/img/video
# 宿主机文件挂载目录
VOLUME /opt/java_app_docker/img/mp4
# 宿主机文件挂载目录
VOLUME /opt/java_app_docker/img/picture
# 宿主机文件挂载目录
VOLUME /opt/java_app_docker/img/gif
VOLUME /opt/java_app_docker/ffmpeg-5.1.1-amd64-static/ffmpeg
# 指定路径
WORKDIR /opt/java_app_docker/app
ADD target/video-de-duplication-0.0.1-SNAPSHOT.jar video.jar
EXPOSE 8083
ENTRYPOINT ["java","-jar","video.jar"]

If you don’t understand, you can read the detailed explanation of the Dockerfile command.

After writing: [Home]->[Project Name]->[Configuration]
Insert image description here
Add execution instructions here

#!/bin/bash 
cd /var/jenkins_home/workspace/video
docker stop video || true #停止容器
docker rm video || true #删除容器
docker rmi video || true #删除镜像
docker build -t video . #编译Dockerfile生成新的进行
docker run -d -p 8083:8083 \ #端口号映射
#内容卷映射
-v /opt/java_app_docker/img/video:/opt/java_app_docker/img/video \
-v /opt/java_app_docker/img/mp4:/opt/java_app_docker/img/mp4 \
-v /opt/java_app_docker/img/picture:/opt/java_app_docker/img/picture \
-v /opt/java_app_docker/img/gif:/opt/java_app_docker/img/gif \
-v /opt/java_app_docker/img/video:/opt/java_app_docker/img/video \
-v /opt/java_app_docker/ffmpeg-5.1.1-amd64-static/ffmpeg:/opt/java_app_docker/ffmpeg-5.1.1-amd64-static/ffmpeg \
-e TZ=Asia/Shanghai \ #设置时区
--name video video:latest
#--name 指定容器名称 
# video:latest指定使用的镜像
(11) After everything is configured, you can click Build again to view the console log. After the final success, just check whether the container is started.

Guess you like

Origin blog.csdn.net/weixin_42600175/article/details/130098761
Recommended