withDockerRegistry step fails with Amazon ECR

Article ID:360001060072
1 minute readKnowledge base

Issue

  • We are trying to push a Docker image to a Amazon ECR repository in a Pipeline, and we see following messages in the console output.

 [docker_container] Running shell script
 + docker tag --force=true my-image:38 my-image:38
 unknown flag: --force
 See 'docker tag --help'.
 + docker tag my-image:38 my-image:38

  The push refers to repository [docker.io/library/my-image]
  c31d9a5958c9: Preparing
  1e9451bf9430: Preparing
  36698d5e0963: Preparing
  f9e79a7653f9: Preparing
  efa0b7a2d37b: Preparing
  fe548f92b224: Preparing
  a7d53ea16e81: Preparing
  e53f74215d12: Preparing
  fe548f92b224: Waiting
  a7d53ea16e81: Waiting
  e53f74215d12: Waiting
  denied: requested access to the resource is denied

Resolution

The tag step fails because the docker version do not know the deprecated parameter --force=true, for more details take a look at JENKINS-42152

There is the bug related to withDockerRegistry step JENKINS-38018, it will not work, so try to use this example that uses docker.withRegistry() instead withDockerRegistry() in it works if the repository "my-image" exists in Amaron ECR repository.

node {
        //cleanup current user docker credentials
        sh 'rm  ~/.dockercfg || true'
        sh 'rm ~/.docker/config.json || true'

        //configure registry
        docker.withRegistry('https://ID.ecr.eu-west-1.amazonaws.com', 'ecr:eu-west-1:86c8f5ec-1ce1-4e94-80c2-18e23bbd724a') {

            //build image
            def customImage = docker.build("my-image:${env.BUILD_ID}")

            //push image
            customImage.push()
        }