Write in front:
In the previous blog, I wrote about the pipeline release of the Java back-end project, but it has not yet involved container operations. This article is about the pipeline release docker image deployment process
Deployment environment
CPU name | IP address | Deploy software | RAM |
---|---|---|---|
harbor | 192.168.154.129 | docker、docker-compose、harbor | 2G |
gitlab | 192.168.154.131 | gitlab 、git、gitlab、mysql | 3G |
jenkins | 192.168.154.128 | jdk、git、jenkins、docker、maven | 2G |
Deployment on the harbor server:
1. Build a harbor warehouse
1. Install harbor warehouse, upload tomcat image
Reference blog https://blog.51cto.com/13760351/2532347
Two, build the database MySQL
1. Install mysql5.7
Reference blog https://blog.51cto.com/13760351/2497200
2. Log in to the database and authorize
mysql -uroot -pMypass@123#!
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Mypass@123#!' with grant option; #设置权限
mysql> flush privileges;
2. Import data
create database quartz
source /etc/quartz.sql
3. View the table
use quartz;
show tables;
note:
If the table name or library name is not recognized, it may be a case of
solution:
vim /etc/my.cnf
#忽略大小写
lower_case_table_names=1
Restart the database
systemctl restart mysqld
Deploy on gitlab server
Build gitlab warehouse
1. Install gitlab repository
Reference blog https://blog.51cto.com/13760351/2467477
2. Upload java code to gitlab warehouse
Reference blog https://blog.51cto.com/13760351/2526052
Deploy on Jenkins server
One, set up Jenkins server
1. Install Jenkins service
Reference blog https://blog.51cto.com/13760351/2368452
2. Install the maven plugin and configuration
Reference blog https://blog.51cto.com/13760351/2527263
3. Install docker
1. Add docker-ce source
cd /etc/yum.repos.d
wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2. Install docker
yum -y install docker-ce
3. Start docker
systemctl start docker
Two, write pipeline script
1. Edited Jenkinsfile
pipeline {
agent {
node {
label 'master'
}
}
// 拉取代码
stages {
stage('GetCode') {
steps {
sleep 5
}
}
// 代码打包
stage('Maven Build') {
steps {
sh '''
export JAVA_HOME=/usr/local/java
/usr/local/apache-maven-3.6.3/bin/mvn clean install -Ptest
'''
}
}
// 推送到镜像仓库
stage('Push Image') {
steps {
sh '''
REPOSITORY=192.168.154.129/maven-test01/maven-quartz:${branch}
//创建镜像
docker build -t $REPOSITORY .
//登录,上传镜像到仓库
docker login 192.168.154.129 -u admin -p Harbor12345
docker push $REPOSITORY
'''
}
}
// 部署到Docker主机
stage('Deploy to Docker') {
steps {
sh '''
//推送shell脚本到docker主机
scp images.sh root@harbor:/root/
//远程执行脚本
ssh -p 22 root@harbor "chmod +x images.sh && sh /root/images.sh"
'''
}
}
}
}
2. Write Dockerfile
#从harbor拉取镜像
FROM 192.168.154.129/maven-test01/tomcat:v1
#镜像构建人信息(可选)
MAINTAINER zhao
#删除webapps目录下文件
RUN rm -rf /usr/local/tomcat/webapps/*
#复制打的最新war包到webapps目录
ADD target/quartz.war /usr/local/tomcat/webapps/
3. Write a mirror shell script
#!/bin/bash
Img=`docker images |grep master |awk -F ' ' '{ print $3}'`
Con=`docker ps |grep :88 |awk -F ' ' '{ print $1}'`
#删除旧的镜像
docker stop $Con
docker rmi -f $Img
#下载新镜像,运行
docker pull 192.168.154.129/maven-test01/maven-quartz:master
sleep 2
#后台运行容器
docker run -d -p 88:8080 192.168.154.129/maven-test01/maven-quartz:master
Three, new project pipline-docker
Four, build test
Five, view the new mirror
Tips:
After the image with the same label is uploaded, the previous image will be automatically overwritten, so there is no need to delete the old image!
Six, view the container operation
docker ps
Seven, java project test
to sum up
1. In fact, three script files have been modified here, provided that they are deployed in other environments.
2. In order to avoid the length of the article, there are many steps, only the blog link address is given, for reference only, if the company already has an environment, you can ignore it.
- Reference document: https://blog.51cto.com/13043516/2365284