【问题标题】:Building docker images inside a Jenkins container在 Jenkins 容器中构建 docker 镜像
【发布时间】:2018-12-04 14:27:51
【问题描述】:

我正在使用 jenkins 容器来执行基于此 Jenkinsfile 的管道:

pipeline {
agent any

tools {
    maven 'Maven 3.6.0'
    jdk 'jdk8'
}
stages {
    stage('Pull from git') {
        steps {
            checkout scm
        }
    }

    stage('Compile App') {
        steps {
            sh "mvn clean install"
        }
    }

    stage('Build da Imagem') {
        steps {
            script {
                docker.withTool("docker") {
                    def readyImage = docker.build("dummy-project/dummy-project-image", "./docker")
                    }
                }
            }
        }
    }
}

在最后阶段,当它尝试构建 docker 映像时,我得到了这个 Error。 是否可以在 jenkins 容器中构建 docker 镜像?

【问题讨论】:

    标签: docker jenkins continuous-integration jenkins-pipeline


    【解决方案1】:

    您的管道执行代理不与 docker daemon 通信,因此您需要正确配置它并且您有三种方式(我知道的那些):

    1) 为您的代理提供 docker 安装

    2) 从https:/$JENKINS_URL/configureTools/ 添加一个 Docker 安装

    3) 如果您使用 Kubernetes 作为编排器,您可以在管道的开头添加一个 podTemplate 定义然后使用它,这里是一个示例:

    // Name of the application (do not use spaces)
    def appName = "my-app"
    // Start of podTemplate
    def label = "mypod-${UUID.randomUUID().toString()}"
    podTemplate(
      label: label,
      containers: [
        containerTemplate(
          name: 'docker',
          image: 'docker',
          command: 'cat',
          ttyEnabled: true)],
      volumes: [
        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
        hostPathVolume(hostPath: '/usr/bin/kubectl', mountPath: '/usr/bin/kubectl'),
        secretVolume(mountPath: '/etc/kubernetes', secretName: 'cluster-admin')],
      annotations: [
          podAnnotation(key: "development", value: appName)]
    )
    // End of podTemplate
    
    
    [...inside your pipeline]
    
      container('docker') {
        stage('Docker Image and Push') {
          docker.withRegistry('https://registry.domain.it', 'nexus') {
          def img = docker.build(appName, '.')
          img.push('latest')
      }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 2018-03-27
      • 2018-06-15
      • 1970-01-01
      • 2020-10-18
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多