Jenkins集成Docker
一、配置凭证
生成令牌
管理凭证
添加凭证
填写信息
二、全局配置
docker-compose
version: '3'
services:
jenkins:
restart: always
image: jenkins:latest
container_name: jenkins
ports:
- '8888:8080'
- '50000:50000'
volumes:
- /home/qiang/docker-compose/jenkins/data/jenkins_home:/var/jenkins_home
- /home/qiang/docker-compose/jenkins/data/war/jenkins.war:/usr/share/jenkins/jenkins.war
查看位置
[root@6e169b02437d jenkins_home]# pwd
/home/qiang/docker-compose/jenkins/data/jenkins_home
[root@6e169b02437d jenkins_home]# mkdir -p software/maven_home
[root@6e169b02437d jenkins_home]# mkdir -p software/jdk_home
settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/var/jenkins_home/software/maven_home/repository</localRepository>
<servers>
<server>
<id>nexus-public</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-public</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Public</name>
<url>http://192.168.8.245:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
添加配置
三、新建任务
构建一个Maven项目
输入项目地址和刚刚添加的凭证
Maven执行打包命令
保存并尝试构建一下
构建成功
四、远程推送
填写服务器信息
配置推送到远程服务器
推送成功
五、构建镜像
开放Docker远程API
# 编辑文件
vim /etc/systemd/system/docker.service
# 修改内容
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
# 重启服务
systemctl daemon-reload && systemctl restart docker
# 测试验证
docker -H 192.168.8.246:2375 images
Jenkins链接Docker
Docker拥有的容器
准备Dockerfile
# 指定基础镜像
FROM 192.168.8.247/xiaoqiangzai/openjdk:8-jre
# 作者的信息
MAINTAINER wuduoqiang
# jar包的名字
ENV jar pavilion-1.0.0.jar
# 工作的目录
ENV workDir /app/
# 进入到容器后进入到哪个目录
WORKDIR ${workDir}
# 将文件拷贝到镜像中
COPY pavilion/target/${jar} ${workDir}
# 设置时区为上海
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 容器启动时候执行的命令
CMD ["sh","-ec","exec java -jar ${jar} 2>&1 >> ${workDir}msg.log"]
构建成功
推送成功
六、定时删除
镜像策略
定时任务
七、启动服务
docker-compose
version: "3.0"
services:
pavilion:
image: 192.168.8.247/xiaoqiangzai/pavilion:{BUILD_ID}
container_name: pavilion
restart: always
privileged: true
ports:
- "8080:8080"
将之前推送的jar包改成推送docker-compose
启动脚本
echo "停止旧服务"
docker stop $(docker ps -aq -f 'name=pavilion') && docker rm $(docker ps -aq -f'name=pavilion')
echo "删掉旧镜像"
docker rmi $(docker images -aq -f 'reference=192.168.8.247/xiaoqiangzai/pavilion:*')
echo "启动新镜像"
eval sed -in 's#{BUILD_ID}#'$BUILD_ID'#' /home/qiang/jenkins/pavilion/docker-compose.yaml
rm -rf /home/qiang/jenkins/pavilion/docker-compose.yamln
docker-compose -f /home/qiang/jenkins/pavilion/docker-compose.yaml up -d
构建成功
上传成功
部署成功
作者(Author):小强崽
来源(Source):https://www.wuduoqiang.com/archives/Jenkins集成Docker
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
版权(Copyright):商业转载请联系作者获得授权,非商业转载请注明出处。 For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source.