基于上一章  基于Jenkins自动打包并部署Tomcat环境_学习新鲜事物的博客-CSDN博客

1、安装docker-ce

在tomcat上创建远程命令

[root@tomcat ~]# mkdir /data

在tomcat上构建 tomcat基础镜像。在构建基础镜像之前需要先安装 Docker 与 JDK。

[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

已加载插件:fastestmirror

adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo

安装dockers启动并查看

[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:           24.0.5

 API version:       1.43

 Go version:        go1.20.6

 Git commit:        ced0996

 Built:             Fri Jul 21 20:39:02 2023

 OS/Arch:           linux/amd64

 Context:           default

Server: Docker Engine - Community

 Engine:

  Version:          24.0.5

  API version:      1.43 (minimum version 1.12)

  Go version:       go1.20.6

  Git commit:       a61e2b4

  Built:            Fri Jul 21 20:38:05 2023

  OS/Arch:          linux/amd64

  Experimental:     false

 containerd:

  Version:          1.6.22

  GitCommit:        8165feabfdfe38c65b599c4993d227328c231fca

 runc:

  Version:          1.1.8

  GitCommit:        v1.1.8-0-g82f18fe

 docker-init:

  Version:          0.19.0

  GitCommit:        de40ad0

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 ~]#  cat centos-7-x86_64.tar.gz | docker import - centos:7

sha256:a94721bf20e6e706ea0ab02bb4283bcd992e760542620aa908143dcf52012e39

[root@tomcat ~]#  docker images

REPOSITORY   TAG       IMAGE ID       CREATED              SIZE

centos       7         a94721bf20e6   3 seconds ago        589MB

3、构建tomcat 基础镜像 

[root@tomcat ~]# mkdir docker-tomcat

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

[root@tomcat docker-tomcat]# ls

[root@tomcat docker-tomcat]# rz

[root@tomcat docker-tomcat]# ll

总用量 196724

-rw-r--r--. 1 root root   9690027 7月  17 2020 apache-tomcat-8.5.40.tar.gz

-rw-r--r--. 1 root root 191753373 7月  17 2020 jdk-8u191-linux-x64.tar.gz

[root@tomcat docker-tomcat]# vim Dockerfile

[root@tomcat docker-tomcat]# cat Dockerfile

FROM centos:7

MAINTAINER from crushlinux <crushlinux@163.com>

#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  .

[+] Building 4.2s (8/8) FINISHED                                                        docker:default

 => [internal] load .dockerignore                                                                 0.0s

 => => transferring context: 2B                                                                   0.0s

 => [internal] load build definition from Dockerfile                                              0.0s

 => => transferring dockerfile: 508B                                                              0.0s

 => [internal] load metadata for docker.io/library/centos:7                                       0.0s

 => CACHED [1/3] FROM docker.io/library/centos:7                                                  0.0s

 => [internal] load build context                                                                 0.1s

 => => transferring context: 9.69MB                                                               0.1s

 => [2/3] ADD ./apache-tomcat-8.5.40.tar.gz /usr/local/                                           0.2s

 => [3/3] ADD ./jdk-8u191-linux-x64.tar.gz /usr/local                                             2.6s

 => exporting to image                                                                            1.3s

 => => exporting layers                                                                           1.3s

 => => writing image sha256:e4cb1349c52e1c727d193642f455b4fbf0e99030edb836d17aca30cfafc7ae61      0.0s

 => => naming to docker.io/library/tomcat:v1   

4构建一个Maven项目

在以上配置完成后,回到 Jenkins 首页,选择“新建任务”,然后输入一个任务名称 “probe-docker”,并选择“Maven project”配置项,点击当前页面下方的“确定”按钮。

 probe-docker

 

在点击“确定”按钮,选择“源码管理”并选中“Git”,设置“Repository URL”地址。

 git@192.168.50.51:/home/git/probe.git

 选择“Build”-> clean package -Dmaven.test.skip=true

 

 在上一步后面选择“构建后操作”中的“Send build artfacts over SSH”选项并进行。

scp 192.168.50.53:/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

以上全部配置完成后,点击保存即可。然后点击刚才创建的工程任务“probe-docker“->” Build new”直至任务构建完成。开始构建过程中可以点击进度条查看 

 

 可以看到此工程任务已构建成功,并且在构建后,创建用于docker项目的 Docker Web 环境命令也执行成功。

[root@tomcat data]# ll

总用量 26616

drwxr-x---. 7 root root       88 8月  25 19:17 probe

-rw-r--r--. 1 root root 27253017 8月  25 19:17 probe.war

[root@tomcat docker-tomcat]# docker ps -a

CONTAINER ID   IMAGE       COMMAND                   CREATED              STATUS              PORTS                                       NAMES

0ed51e281414   tomcat:v1   "/bin/sh -c '/usr/lo…"   About a minute ago   Up About a minute   0.0.0.0:8090->8080/tcp, :::8090->8080/tcp   tomcat-test

 浏览器访问http://192.168.50.54:8090/probe/

登陆进容器

[root@tomcat docker-tomcat]#  docker exec -it 0 /bin/bash

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

 44   <role rolename="manager-gui"/>

 45   <role rolename="admin-gui"/>

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

 47 </tomcat-users>        在这一行上面加三行

重载启动

[root@tomcat data]# systemctl daemon-reload

[root@tomcat docker-tomcat]# systemctl restart docker

会挂掉从新启动就好了

[root@tomcat docker-tomcat]# docker ps -a

CONTAINER ID   IMAGE       COMMAND                   CREATED         STATUS                        PORTS     NAMES

0ed51e281414   tomcat:v1   "/bin/sh -c '/usr/lo…"   4 minutes ago   Exited (137) 17 seconds ago             tomcat-test

[root@tomcat docker-tomcat]# docker start 0

0

[root@tomcat docker-tomcat]# docker ps -a

CONTAINER ID   IMAGE       COMMAND                   CREATED         STATUS        PORTS                                       NAMES

0ed51e281414   tomcat:v1   "/bin/sh -c '/usr/lo…"   5 minutes ago   Up 1 second   0.0.0.0:8090->8080/tcp, :::8090->8080/tcp   tomcat-test

 用户  tomcat

密码  tomcat

 看到这里就成功了

 可能错误

把容器删除从新创建

SSH: Connecting from host [jemkins]
SSH: Connecting with configuration [web] ...
SSH: EXEC: completed after 608 ms
SSH: Disconnecting configuration [web] ...
ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [125]]
Build step 'Send build artifacts over SSH' changed build result to UNSTABLE
Finished: UNSTABLE
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐