Automatically package and deploy docker environment based on Jenkins

Table of contents

1. Install docker-ce

2. Alibaba Cloud Image Accelerator

3. Build tomcat base image 

4. Build a Maven project


lab environment

operating system

IP address

CPU name

Role

CentOS7.5

192.168.200.111

git

git server

CentOS7.5

192.168.200.112

Jenkins git client

jenkins server

CentOS7.5

192.168.200.113

docker

web server

1. Install docker-ce


Create a remote directory on 192.168.200.113.

[root@tomcat ~]# mkdir /data

On the 192.168.200.113 machine, build the tomcat base image. Docker and JDK need to be installed before building the base image.

[root@tomcat ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@tomcat ~]# yum -y install yum-utils device-mapper-persistent-data lvm2

[root@tomcat ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@tomcat ~]# ls /etc/yum.repos.d/

[root@tomcat ~]# yum -y install docker-ce

 

[root@tomcat ~]# systemctl start docker
[root@tomcat ~]# systemctl enable docker

 

[root@tomcat ~]# docker version

 

2. Alibaba Cloud Image Accelerator


[root@tomcat ~]# cat << END > /etc/docker/daemon.json
 {
         "registry-mirrors":[ "https://nyakyfun.mirror.aliyuncs.com" ]
 }
END

 

[root@tomcat ~]# systemctl daemon-reload
[root@tomcat ~]# systemctl restart docker

Import centos 7

 

 

[root@tomcat ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7

[root@tomcat ~]# docker images

 

3. Build tomcat base image 


[root@tomcat ~]# mkdir docker-tomcat
[root@tomcat ~]# cd docker-tomcat/

During this period import: apache-tomcat-8.5.40.tar.gz and jdk-8u191-linux-x64.tar.gz

 

[root@tomcat docker-tomcat]# vim dockerfile

 FROM centos:7

MAINTAINER from crushlinux <[email protected]>

#copy jdk and tomcat into image

ADD ./apache-tomcat-8.5.40.tar.gz /usr/local/

ADD ./jdk-8u191-linux-x64.tar.gz /usr/local

#set variable

ENV JAVA_HOME /usr/local/jdk1.8.0_191

ENV PATH $JAVA_HOME/bin:$PATH

#container starts up

ENTRYPOINT /usr/local/apache-tomcat-8.5.40/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.40/logs/catalina.out

 

[root@tomcat docker-tomcat]# docker build -t tomcat:v1  .

 

4. Build a Maven project


After the above configuration is completed, return to the Jenkins homepage, select "New Task", then enter a task name "probe-docker", select the "Maven project" configuration item, and click the "OK" button at the bottom of the current page.

 

 

 After clicking the "OK" button, select "Source Code Management" and select "Git" to set the "Repository URL" address.

 

Select "Build" ->  clean package -Dmaven.test.skip=true

 

 

Select the "Send build artfacts over SSH" option in "Post-Build Actions" after the previous step and proceed.

 scp 192.168.200.112:/root/.jenkins/workspace/probe-docker/psi-probe-web/target/probe.war /data/

docker run -itd --name tomcat-test -p 8090:8080 -v /data:/usr/local/apache-tomcat-8.5.40/webapps tomcat:v1 

 

 

After all the above configurations are completed, click Save. Then click "probe-docker"->"Build new" on the engineering task you just created until the task is built. You can click on the progress bar to view the build process.

 

 

 You can see that this engineering task has been built successfully, and after the build, the command to create a Docker Web environment for the docker project has also been executed successfully.

[root@tomcat ~]# ls /data

[root@tomcat ~]# docker ps -a

 [root@tomcat ~]#docker exec -it 5e /bin/bash

[root@5eaecb826ff6 /]# vim /usr/local/apache-tomcat-8.5.40/conf/tomcat-users.xml

<role rolename="manager-gui"/>

  <role rolename="admin-gui"/>

  <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>

</tomcat-users> # Add the above three lines before this line

 

 

[root@5eaecb826ff6 /]# cd /usr/local/apache-tomcat-8.5.40/conf

[root@5eaecb826ff6 conf ]# /usr/local/apache-tomcat-8.5.40/conf/

 [root@5eaecb826ff6 conf ]# /usr/local/apache-tomcat-8.5.40/bin/startup.sh

 

Visit: 192.168.200.113:8090/prode

User: tomcat

Password: tomcat

 

 

Guess you like

Origin blog.csdn.net/2302_77750172/article/details/132449247