Docker builds Jenkins + Maven code to build deployment platform

Time:2021-11-24

Due to project development, it is often necessary to set up some local environments for development or testing. As we all know, setting up environments, installing software and installing middleware is a very troublesome thing. The installation of some software may depend on the installation of other environments. For example, installing rabbitmq first requires Erlang language environment. A complete set of installation is not only a waste of time, but also various problems, Seriously affect the development progress. For developers, life is short, do everything possible to improve development efficiency.

The emergence of docker is undoubtedly an epoch-making event for the technical community. Docker is deeply loved by the majority of IT practitioners because of its rich application image warehouse, convenient use and portability. Using docker to deploy or install corresponding applications does not need to spend too much time on the installation process, details and dependencies, so as to truly use out of the box. Taking Jenkins as an example, this paper explains the basic operation process of docker: pulling image, running container, file mounting, tracking operation, closing container, etc., hoping to be helpful to the technical enthusiasts who have just started docker.

Basic concepts of docker

Docker is an open source application container engine, which allows developers to package their applications and dependency packages into a portable container, and then publish them to any popular Linux machine. In fact, the most classic picture of docker is the picture of “dolphin with container on its back”, which fully illustrates the relationship between docker engine and container.
After mastering image, container and warehouse, you can basically understand docker and use docker correctly

  • Image: it can be understood as the “source code” or.exeInstallation documents;
  • Container: the instance after the image is run. The relationship with the image is actually like the relationship between “object” and “class”;
  • Warehouse: the warehouse that publishes images. Various images (Applications) can be downloaded or pulled in the warehouse. Docker is the engine required for container operation, and all containers run in the engine.

Docker installation process (centos6.9)

Upgrade kernel

The kernel version of CentOS 6.9 needs to be upgraded before docker can be installed. The upgrade process is as follows
1. Import key


rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 
rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm 

2. Yum installation


yum --enablerepo=elrepo-kernel -y install kernel-lt 

3. Guide file modification(grub.conf


vim /etc/grub.conf 

Set default to 0, default = 0

4. Restart

Install docker IO

1. Install the EPEL source


yum install http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm 

2. Install docker IO


yum -y install docker-io 

3. Start docker service


service docker start

Jenkins installation based on docker

Pull application

Before running the container / instance, you need to pull the corresponding image from the docker hub warehouse. You can view the official website aboutJenkins mirrorThe installation command, configuration information and file directory are described in detail.
Pull the latest Jenkins from the warehouse,docker pull jenkins, you can use the command to pull other versionsdocker pull jenkins:<version>

Run container

When running a container / instance, you need to consider the mapping between the local port and the docker container so that it can be accessed through the local host. Because the container itself does not persist files, docker does not recommend any file related write operations in the container, but only as a “container”. If many important data needs to be saved or configured during the running of container applications, such as MySQL DB files, Jenkins plug-ins, configuration files, etc., it is recommended to mount the container files locally, that is, volume mapping.  


docker run -p 8080:8080 -p 50000:50000 -v /usr/local/docker_volume/jenkins:/var/jenkins_home -v /usr/local/apps/maven-3.3.9:/var/maven_home -v /usr/local/apps/maven-3.3.9/repo:/usr/local/apps/maven-3.3.9/repo --name my_jenkins -d jenkins

  Where – P represents port mapping, that is, the port on the local machine is mapped to the port on the container;
-V means file mapping, that is, the local machine shares the mapping with the container and mounts the file
be careful: because Jenkins needs to rely on the Maven project to build the Maven project, the native Maven will be used when running the container_ Home is shared to the container. Since the container needs to be written in the local shared file when running, the write permission is required,sudo chown -R 1000:1000 /usr/local/docker_volume/jenkins sudo chown -R 1000:1000 sudo chown -R 1000:1000

Run the above command, and the window will print the container IDdocker psYou can view the running container status and related information.

Trace application log

inputdocker logs <CONTAINER ID>You can track the container and print the log. When Jenkins is started for the first time, the password will be printed in the log and copied to log in to Jenkins

Close open container

close:docker stop <CONTAINER ID>
Open:docker start <CONTAINER ID>

Tomcat account configuration

This example deploys the war compiled by Maven to the remote tomcat, so you need to set the deployment permission in Tomcat and edit it/con/tomcat-users.xmlProfile, add account


<role rolename="tomcat"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="admin,manager,tomcat,manager-gui,manager-script,manager-jmx,manager-status"/>
<user username="root" password="admin" roles="admin,manager,tomcat,manager-gui,manager-script,manager-jmx,manager-status,admin-gui"/>

Configure Jenkins, build and deploy war applications

So far, by visitingHttp: // native IP: 8080You can access the Jenkins platform, select the default installation plug-in, and set the account after successful installation

Configure parameters such as JDK and Maven

Add JDK installation in “system settings – > global tool configuration”, as shown in the figure below

set01

set_02

set_03

Install the “deploy to container plugin” plug-in to deploy the war program
Install in system settings – > management plug-ins – > optional plug-ins

New task build deployment

Add SVN source code address and Tomcat deployment address

set_04

set_07

Tomcat deployment configuration: configure the account to log in to Tomcat

set_06

set_08

Deploy Maven project to specified Tomcat through SSH script

Using the above method to implement deployment in the deployment built-in of Tomcat consumes a lot of server performance, and problems such as memory overflow often occur, resulting in deployment failure. SSH script deployment is much more convenient and fast, and the memory consumption is not so much.

Configure the publish over SSH plug-in

Install the (publish over SSH) plug-in in system settings – > management plug-ins – > optional plug-ins, and then set the login information of the Linux server to be published in system management – > system settings – > publish over SSH. The configurations and specific meanings are shown in the following figure. After configuration, click the [test configuration] button to test whether it is connected. If “success” is displayed, the configuration is successful.

ssh登录

Writing deployment SSH scripts

The following shell script will bedeploy.shStored on the server/home/admin/dataDirectory and give execution permissionchomd +x deploy.sh, as shown in the SSH login configuration diagram aboveRemote DirectoryDirectory. This configuration indicates that Jenkins is available after logging in to the Linux server where Tomcat is located./deploy.shPerform the deployment operation. Ifdemploy.shIf it is placed in another directory, the absolute path is used/..dir../..dir../deploy.shTo execute.

#!/bin/bash
#File: set FF = UNIX
#defined 
export JAVA_HOME=/usr/local/java
#Tomcat deployment location
TOMCAT_HOME="/home/admin/data/tomcat-jenkins"
#The logged in server user directory is the same as the remote directory settings configured above
LOGIN_HOME="/home/admin/data"
TOMCAT_PORT=10092
PROJECT="$1"
#param validate
if [ $# -lt 1 ]; then
  echo "you must use like this : ./deploy.sh <projectname> [tomcat port] [tomcat home dir]"  
  exit
fi
if [ "$2" != "" ]; then
   TOMCAT_PORT=$2
fi
if [ "$3" != "" ]; then
   TOMCAT_HOME="$3"
fi
#shutdown tomcat
#"$TOMCAT_HOME"/bin/shutdown.sh
#echo "tomcat shutdown"
#check tomcat process

#tomcat_pid=`/usr/sbin/lsof -n -P -t -i :$TOMCAT_PORT`
tomcat_pid=`ps -ef | grep $TOMCAT_HOME | grep -v 'grep\|tail\|more\|bash\|less'| awk '{print $2}'`
echo "current :" $tomcat_pid
while [ -n "$tomcat_pid" ]
do
 sleep 5
 tomcat_pid=`ps -ef | grep $TOMCAT_HOME | grep -v 'grep\|tail\|more\|bash\|less'| awk '{print $2}'`
 echo "scan tomcat pid :" $tomcat_pid
 if [ -n "$tomcat_pid" ]; then
   echo "kill tomcat :" $tomcat_pid
   kill -9 $tomcat_pid
 fi
done
#publish project
echo "scan no tomcat pid,$PROJECT publishing"
rm -rf "$TOMCAT_HOME"/webapps/$PROJECT
cp "$LOGIN_HOME"/war/$PROJECT.war "$TOMCAT_HOME"/webapps/$PROJECT.war
#bak project
#BAK_DIR=/home/web_as/war/bak/$PROJECT/`date +%Y%m%d`
#mkdir -p "$BAK_DIR"
#cp "$TOMCAT_HOME"/webapps/$PROJECT.war "$BAK_DIR"/"$PROJECT"_`date +%H%M%S`.war
#remove tmp
rm -rf "$LOGIN_HOME"/war/$PROJECT.war
#start tomcat
"$TOMCAT_HOME"/bin/startup.sh
echo "tomcat is starting,please try to access $PROJECT conslone url"

Project build configuration

After saving the configuration according to the figure below, you can build the Maven project, and Jenkins will copy the compiled war project to192.168.1.12Under the Tomcat path corresponding to the server, and then start the Tomcat deployment project.

项目构建配置

This is the end of this article about docker building Jenkins + Maven code to build the deployment platform. For more information about docker building Jenkins + maven, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Seven solutions for distributed transactions

1、 What is distributed transaction Distributed transaction means that transaction participants, transaction supporting servers, resource servers and transaction managers are located on different nodes of different distributed systems. A large operation is completed by more than n small operations. These small operations are distributed on different services. For these operations, either all of them are […]