Jenkins 生成Dockerfile并构建镜像

304 阅读1分钟

1. 创建 Jenkins 任务

  • 首先,生成 Base 基础镜像 Dockerfile
  • 其次,据生成的 Dockerfile,基于 cantos 镜像,生成 Base 基础镜像
  • 最后,推送至镜像仓库

1.1. 创建流水线任务

  • 定义 Pipline script
    pipeline {
        agent any
        stages {
            stage('Generate Base Dockerfile') {
                steps {
                    script {
                        def dockerfileContent = """
                        FROM centos7.9.2009
                        ENV LANG en_US.UTF-8
    
                        RUN cp /usr/share/zoneinfo/Asia/Pontianak /etc/localtime \\
                        && echo 'LANG="en_US.UTF-8"' > /etc/locale.conf \\
                        && sed -i 's/^*/#*/g' /etc/security/limits.d/20-nproc.conf \\
                        && yum install -y epel-release \\
                        && yum install -y cronolog python-pip net-tools initscripts iproute vim \\
                        && yum clean all \\
                        && curl -sL -o /usr/bin/gosu "https://github.com/tianon/gosu/releases/download/1.10/gosu-amd64" \\
                        && chmod +x /usr/bin/gosu
                        """
                        writeFile file: '/root/Dockerfile/Dockerfile', text: dockerfileContent
                    }
                }
            }
            
            stage('Build Base Image') {
                steps {
                     dir("/root/Dockerfile/"){
                         script {
                             def imageName = 'harbor.example.com/base/centos'
                             def imageTag = 'base'
                             def dockerImage = docker.build("${imageName}:${imageTag}")
                             dockerImage.push()
                         }
                    }
                }
            }
    }
    

2. 测试验证

企业微信截图_169114761050.png