Jenkins/Docker: 如何在构建前强制拉取基础镜像

6
Docker允许在docker build命令中使用--pull标志,例如:docker build --pull -t myimage .。 我该如何在我的Jenkinsfile管道脚本中强制拉取基础镜像?这样我想确保构建始终使用最新的容器映像,而不受本地版本的影响。
node('docker') {
    def app

    stage('Checkout') {
        checkout scm
    }

    stage('Build image') {
        docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
            app = docker.build "myimage"
        }
    }

    stage('Publish image') {
        docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
            app.push("latest")
        }
    }
}

https://issues.jenkins-ci.org/browse/JENKINS-55821 - kivagant
3个回答

11
最直接的答案是使用docker.build的第二个参数。
stage('Build image') {
    docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
        app = docker.build("myimage", "--pull .")
    }
}
如果您不提供上下文,那么它将默认为.,因此如果您传递任何内容,则必须自己包含上下文。
您可以在“Pipeline Syntax - 全局变量参考”中找到此内容。只需将/pipeline-syntax/globals添加到任何Jenkins URL的结尾即可(例如:http://localhost:8080/job/myjob/pipeline-syntax/globals)。

5

additionalBuildArgs可以完成任务。

示例:

pipeline {
    agent {
        label "docker"
    }

    stages {
        […]

        stage('Build image') {
            agent {
                dockerfile {
                    reuseNode true
                    registryUrl "https://registry.comapny.com"
                    registryCredentialsId "dcr-jenkins"
                    additionalBuildArgs "--pull --build-arg APP_VERSION=${params.APP_VERSION}"
                    dir "installation/app"
                }
            }

            steps {
                script {
                    docker {
                        app = docker.build "company/app"
                    }
                }
            }
        }

        […]
    }

}

0
在你的脚本开始处添加 docker rmi <image>,在 docker build --pull 之前。当 docker build --pull 执行时,该镜像在本地不存在,因此每次都会重新下载。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接