1、安装 docker-ce
在 192.168.200.128 机器上,构建 tomcat 基础镜像。在构建基础镜像之前需要先安装 Docker 与 JDK。
[root@tomcat ~]# cd /etc/yum.repos.d/
[root@tomcat yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@tomcat ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@tomcat ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
[root@tomcat ~]# yum -y install docker-ce
[root@tomcat ~]# systemctl start docker
[root@tomcat ~]# systemctl enable docker[root@tomcat ~]# docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:27:04 2020
OS/Arch: linux/amd64
Experimental: falseServer: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:25:42 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
2、配置阿里云镜像加速器
[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
导入镜像
[root@tomcat ~]# ls
anaconda-ks.cfg centos-7-x86_64.tar.gz
apache-tomcat-8.5.16.tar.gz jdk-8u191-linux-x64.tar.gz
[root@tomcat ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7
sha256:b888dff214910664c0556e903d771339dd5151a647b302ba51683153a12a3f8f
[root@tomcat ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 b888dff21491 50 seconds ago 589MB
3、构建 tomcat 基础镜像
[root@tomcat ~]# mkdir docker-tomcat
[root@tomcat ~]# mv apache-tomcat-8.5.16.tar.gz jdk-8u191-linux-x64.tar.gz docker-tomcat/
[root@tomcat ~]# cd docker-tomcat/
[root@tomcat docker-tomcat]# ls
apache-tomcat-8.5.16.tar.gz jdk-8u191-linux-x64.tar.gz[root@tomcat docker-tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER from wxl <wxl25678@163.com>#copy jdk and tomcat into image
ADD ./apache-tomcat-8.5.16.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.16/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.16/logs/catalina.out[root@tomcat docker-tomcat]# docker build -t tomcat:v1 ./
Sending build context to Docker daemon 201.2MB
Step 1/7 : FROM centos:7
---> b888dff21491
Step 2/7 : MAINTAINER from wxl <wxl25678@163.com>
---> Running in e1376aefb5ab
Removing intermediate container e1376aefb5ab
---> c86457cccbef
Step 3/7 : ADD ./apache-tomcat-8.5.16.tar.gz /usr/local/
---> a10a8ab77866
Step 4/7 : ADD ./jdk-8u191-linux-x64.tar.gz /usr/local/
---> bba75bfa4785
Step 5/7 : ENV JAVA_HOME /usr/local/jdk1.8.0_191
---> Running in ab087e577835
Removing intermediate container ab087e577835
---> 8a67035d5dae
Step 6/7 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 989d1d387785
Removing intermediate container 989d1d387785
---> 3811d389adad
Step 7/7 : ENTRYPOINT /usr/local/apache-tomcat-8.5.16/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.16/logs/catalina.out
---> Running in d42dd67c4f7e
Removing intermediate container d42dd67c4f7e
---> d1cf60c96bb6
Successfully built d1cf60c96bb6
Successfully tagged tomcat:v1[root@tomcat docker-tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v1 d1cf60c96bb6 About a minute ago 999MB
centos 7 b888dff21491 19 minutes ago 589MB
4、构建一个 Maven 项目
在以上配置完成后,回到 Jenkins 首页,选择 “新建任务”,然后输入一个任务名称 “probe-docker”,并选择 “Maven project” 配置项,点击当前页面下方的 “确定” 按钮。
在点击 “确定” 按钮,选择 “源码管理” 并选中 “Git” ,设置 “Repository URL” 地址。
选择 “Build”
在上一步后面选择 “构建后操作” 中的 “Send build artfacts over SSH” 选项并进行。
scp 192.168.200.117:/var/lib/jenkins/workspace/probe/psi-probe-web/target/probe.war /data/
docker run -itd --name tomcat-test -p 8090:8090 -v /data:/usr/local/apache-tomcat-8.5.16/webapps tomcat:v1
以上全部配置完成后,点击保存即可。然后点击刚才创建的工程任务 “probe-docker” -> “Build new” 直至任务构建完成。开始构建过程中可以点击进度条查看。
可以看到此工程任务已构建成功,并且在构建后,创建用于 docker 项目的 Docker Web 环境命令也执行成功。
[root@tomcat ~]# ls /data
probe probe.war
[root@tomcat ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bc147ad2195 tomcat:v1 "/bin/sh -c '/usr/lo…" 2 minutes ago Up 2 minutes 0.0.0.0:8090->8090/tcp tomcat-test