Jenkins/Docker: How to force pull base image before build
11,192
Solution 1
additionalBuildArgs does the job.
Example:
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"
}
}
}
}
[…]
}
}
Solution 2
The most straight-forward answer is to use the second argument to docker.build
stage('Build image') {
docker.withRegistry('https://myregistry.company.com', 'dcr-jenkins') {
app = docker.build("myimage", "--pull .")
}
}
If you don't supply it then it just defaults to .
, so if you pass in anything you must also include the context yourself.
You can find this in the "Pipeline Syntax - Global Variable Reference". Just add /pipeline-syntax/globals
to the end of any Jenkins URL (i.e. http://localhost:8080/job/myjob/pipeline-syntax/globals)
Author by
Stephan
Updated on June 29, 2022Comments
-
Stephan almost 2 years
Docker allows passing the
--pull
flag todocker build
, e.g.docker build --pull -t myimage .
. How can I enforce pulling the base image with a pipeline script in myJenkinsfile
? This way I want to ensure that the build always uses the latest container image despite of the version available locally.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") } } }