docker安装jenkins+tomcat+git自动化部署

docker 安装jenkins

前言
命令参考:docker命令传送门

1.创建目录

mkdir -p /home/jenkins

2.授权

chown -R 1000 /var/jenkins

3.获取最新jenkins

docker pull jenkins/jenkins

查看本地仓库名:docker images

1
2
3
[root@izuf6dtic2d71rbvtn6126z home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jenkins/jenkins latest 08b7b3e99b5a 7 days ago 566MB

jenkins启动:
docker run -p 8088:8080 -p 50000:50000 -itd -v /home/jenkins:/var/jenkins_home -v /opt/maven/apache-maven-3.6.0/:/var/maven/ -v /usr/local/java/jdk1.8/:/var/java/ -v /etc/localtime:/etc/localtime --name jenkins jenkins/jenkins

参数解释:

-d 后台运行镜像-p 8088:8080 将服务器的8088端口映射到容器的8080端口

-p 50000:50000 将服务器的50000端口映射到容器的50000端口

-v /home/jenkins:/var/jenkins_home 将硬盘上的/home/jenkins挂载到容器内部的/var/jenkins_home目录,方便后续更新镜像后继续使用原来的工作目录。
其余的同理

-v /etc/localtime:/etc/localtime 让容器使用和服务器同样的时间设置。

–name jenkins 给容器起一个别名
(每一个镜像启动后都是一个单独的容器,所以各个镜像映射的内部端口号一般不会冲突,除非你启动两个一样的镜像。因为外部端口号是基于linux的,所以注意占用和防火墙以及服务器的安全组)

-p 参数的几种用法:

hostPort:containerPort
这种用法是将宿主机端口和容器端口绑定起来

ip:hostPort:containerPort
这种是将指定的 ip 地址的端口和容器的端口进行映射

ip::containerPort
将指定 ip 地址的随机端口映射到容器的开放端口上

查看初始密码:
1.进入容器内部执行
cat /var/jenkins_home/secrets/initialAdminPassword

插件下载(系统管理–插件管理–搜索下载):
构建maven项目: Maven Integration plugin
部署到tomcat:Deploy to container Plugin

docker安装tomcat

1.拉取

docker pull tomcat

2.启动

docker run -d -p 8888:8080 --name tomcat tomcat

可能会用到

docker 容器内部安装vim

1.进入容器后执行命令apt-get update下源库
2.然后执行apt-get install vim命令


springboot的jar改成war包运行

1.修改pom文件,jar改成war
<packaging>war</packaging>
2.启动类 重写其configure方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Application extends SpringBootServletInitializer {
/**
* Configure the application. Normally all you would need to do is to add sources
* (e.g. config classes) because other settings have sensible defaults. You might
* choose (for instance) to add default command line arguments, or set an active
* Spring profile.
*
* @param builder a builder for the application context
* @return the application builder
* @see SpringApplicationBuilder
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return super.configure(builder);
}


public static void main(String[] args) {
long start = System.currentTimeMillis();
SpringApplication.run(Application.class, args);
log.info("启动完成,耗时:" + (System.currentTimeMillis() - start) + "ms");
}
}


tomcat 设置默认项目

在tomcat目录/conf下面修改文件service.xml里面的host标签里面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Host name="localhost"  appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- 新增 -->
<Context path="" docBase="项目名称" reloadable="true"/>

<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->

<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />

</Host>


docker(18.09.6)+jenkins(2.180)+tomcat(8.5)自动部署

1.配置环境

如maven,java,我配置的容器外部环境,注意:启动jenkins时要把你需要的环境路径挂载到容器内,不然这里直接填外部路径会找不到
比如:/xxx/jdk/ is not a directory on the Jenkins master (but perhaps it exists on some agents)
这里也可以偷懒直接使用工具提供的安装, 安装你所需要的环境
config

2.安装插件

插件管理里下载:Maven Integration plugin

3.新建任务 - 构建一个maven项目

3.1 配置git仓库地址,并填写git的账户名密码

3.2 构建触发器

使用轮询scm H/30 * * * * 每隔30分钟向git请求,检查代码是否改变,如果有改变则自动部署

3.3 配置 bulid 项

bulid

3.4 配置构建后

选择 deploy war/ear to a container
这里 tomat必须安装在容器内部,不然访问不到。

WAR/EAR files : 指打包后文件相对于jenkins的项目下所在位置。(如下图)

1
2
3
4
jenkins@23ec9256a404:~/workspace/myapp$ pwd
/var/jenkins_home/workspace/myapp
jenkins@23ec9256a404:~/workspace/myapp$ ls
pom.xml spring-security.iml src target

Context path相关配置入图:
构建后

tomcat相关修改

进入tomcat容器内,添加用户名密码
tomcat-user
1.进入tomcat容器docker exec -it tomcat /bin/bash
2.编辑 vim conf/tomcat-users.xml
3.添加以下代码块

1
2
3
4
5
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="tomcat" password="tomcat" roles="manager-status,manager-gui,manager-script,manager-jmx"/>

4.修改tomcat/webapps/manager/META-INF/context.xml ,放行你的服务器地址,或者直接注释掉这个标签

1
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|你的服务器地址" />

最后应用,保存,立即构建,构建成功即可。

-------------笔者水平有限,若有错漏,欢迎指正!-------------

本文标题:docker安装jenkins+tomcat+git自动化部署

文章作者:yh

发布时间:2019年06月24日 - 13:06

最后更新:2019年06月24日 - 13:06

原始链接:https:www.yh0729.cn/20190624/docker-jenkins.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!