4

I am working with Jenkins. I am trying to push image to ECR. I am using local Docker to build the images.

Below is my Jenkins file:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                bat  'docker build -t sampleapp -f SampleApp/Dockerfile .'
            }
        }
        stage('Push image') {
         steps {
           withDockerRegistry([url: "https://536703334988.dkr.ecr.ap-southeast-2.amazonaws.com/test-repository",credentialsId: "ecr:ap-southeast-2:demo-ecr-credentials"]) {
           bat 'docker push sampleapp:latest'
               }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

In the above code, I am able to build and create an image. In the second stage, I am facing the issues. I am getting the below error:

$ docker login -u AWS -p ******** https://536703334988.dkr.ecr.ap-southeast-2.amazonaws.com/test-repository

WARNING! Using --password via the CLI is insecure. Use --password-stdin.  
Login Succeeded
C:\Program Files (x86)\Jenkins\workspace\SampleAppPipeLine>docker push sampleapp:latest 
The push refers to repository [docker.io/library/sampleapp]
a160522d6d0e: Preparing
2e2c2606bd45: Preparing
9b0a482c69b1: Preparing
995a0cc6a5f6: Preparing
c1b55dcb46c2: Preparing
cf5b3c6798f7: Preparing
cf5b3c6798f7: Waiting
    
denied: requested access to the resource is denied

Can someone help me to fix this issue? Any help would be appreciated.
Thanks.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Niranjan
  • 1,881
  • 6
  • 44
  • 71
  • I did not see any error in the logs? – Adiii Oct 01 '19 at 09:13
  • Hi process apparently never started message it will display and stucks. Also in work space templ folder will get create and file script.sh will get create. Inside script.sh I can see docker tag sampleapp 536703334988.dkr.ecr.ap-southeast-2.amazonaws.com/sampleapp:latest – Niranjan Oct 01 '19 at 09:24
  • May I know http://536703334988.dkr.ecr.ap-southeast-2.amazonaws.com/sampleapp:latest this is the url supposed to be inside script.sh? – Niranjan Oct 01 '19 at 09:27
  • yes if this your AWS ECR url – Adiii Oct 01 '19 at 09:32
  • But where to change this? docker.withRegistry already I gave with https – Niranjan Oct 01 '19 at 09:34

1 Answers1

3

Default repository of docker.io is being hardcode is : docker.io/library/

So for AWS ECR repo, you should :

docker build -t test-repository .

docker tag test-repository:latest 536703334988.dkr.ecr.ap-southeast-2.amazonaws.com/test-repository:latest

docker push 536703334988.dkr.ecr.ap-southeast-2.amazonaws.com/test-repository:latest

Make sure test-repository repo is already create on ECR.

Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53