I’m trying to set up a Jenkins pipeline that builds Docker images using Amazon ECR as a remote cache. My agents are temporary, so I want to boost cache hits. Here’s what I’ve got so far:
myBuilder = 'ecr-cache-builder'
pipeline {
stages {
stage('Build') {
steps {
script {
// Set up a custom builder
sh """
docker buildx inspect ${myBuilder} || \
docker buildx create \
--driver=docker-container \
--name=${myBuilder}
"""
// Check that the builder exists
sh 'docker buildx ls'
// Build with the ECR registry
docker.withRegistry("https://${ECR_URL}", "ecr:${REGION}:${ECR_CREDS}") {
docker.build("${IMAGE_NAME}_${BRANCH}_${BUILD_NUM}",
"--builder ${myBuilder} \
--cache-to mode=max,image-manifest=true,oci-mediatypes=true,type=registry,ref=${ECR_URL}/${IMAGE_NAME}:cache \
--cache-from type=registry,ref=${ECR_URL}/${IMAGE_NAME}:cache \
."
)
}
}
}
}
}
}
I set up a Docker Builder with a docker-container
driver to support the use of ECR as a cache. The default docker
driver doesn’t offer this functionality. Although the builder appears when I run docker buildx ls
, the docker.build()
step fails with a message saying the builder wasn’t found. Any thoughts on what might be going wrong?