Jenkins集成Docker

Posted 小强崽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins集成Docker相关的知识,希望对你有一定的参考价值。

一、配置凭证

生成令牌

image-20210601223825076

管理凭证

image-20210602233028424

添加凭证

image-20210602233059146

填写信息

image-20210601224126260

二、全局配置

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

image-20210602223756511

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>

添加配置

image-20210602231637156

三、新建任务

构建一个Maven项目

image-20210602223207026

输入项目地址和刚刚添加的凭证

image-20210602233225432

Maven执行打包命令

image-20210605010923062

保存并尝试构建一下

image-20210602234908612

构建成功

image-20210602234840038

四、远程推送

填写服务器信息

image-20210603001522661

配置推送到远程服务器

image-20210603003024813

推送成功

image-20210603002241191

五、构建镜像

开放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

image-20210605011756086

Jenkins链接Docker

image-20210605012103260

Docker拥有的容器

image-20210605021333640

准备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"]

image-20210605234158760

构建成功

image-20210605234421458

推送成功

image-20210605234551290

六、定时删除

镜像策略

image-20210605234940257

定时任务

image-20210605235246065

七、启动服务

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

image-20210606023359407

启动脚本

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

构建成功

image-20210606023821642

上传成功

image-20210606023726973

部署成功

image-20210606023641732

作者(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.

以上是关于Jenkins集成Docker的主要内容,如果未能解决你的问题,请参考以下文章

jenkins+webhook+docker做持续集成

CI与CD之Docker上安装Jenkins

CI与CD之Docker上安装Jenkins

gitlab+jenkins+maven+docker持续集成——sonarqube及sonarscanner代码审查

Jenkins+Docker+SpringCloud微服务持续集成(中)

Jenkins—— Jenkins+Docker+SpringCloud微服务持续集成项目优化和集群