Site icon Vinsguru

DevOps – Pushing Docker Image Into ECR

Overview:

Most of the organizations use amazon cloud AWS. So naturally we might want to use Elastic Container Registry (ECR) to store the docker images.In order to push the docker images into ECR, we need some credentials. Some of us create an IAM user and store that in the CI server like Jenkins. It is not really a good practice to create an IAM user. We should be always using some sort of Role to provide the short term token to authenticate the user.  In this article, Lets see how we can create a docker repository for our docker image and push it into ECR using docker-credential-helper.

Prerequisites:

I assume you already have the following!

Sample Docker Image:

Lets first create a simple docker image. I am planning to use my existing project here in GitHub. Feel free to fork/clone to play with this.

Creating Repository In AWS:

pipeline {
    agent none
    stages {
        stage('Build Jar') {
            agent {
                docker {
                    image 'maven:3-alpine'
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh 'mvn clean package -DskipTests'
            }
        }
        stage('Build Image') {
            steps {
                script {
                    app = docker.build("12345678.dkr.ecr.ca-central-1.amazonaws.com/selenium-docker")
                }
            }
        }
        stage('Push Image') {
            steps {
                script {
                    app.push("latest")
                }
            }
        }
    }
}

git clone https://github.com/awslabs/amazon-ecr-credential-helper
cd amazon-ecr-credential-helper
make docker

The above make docker command might take some time. Be patient. Once it is complete, below binary file would have been created. Run the below command to move it to the PATH.

sudo cp ./bin/local/docker-credential-ecr-login /usr/bin/docker-credential-ecr-login
vi ~/.docker/config.json
"credsStore": "ecr-login"
{
    "credsStore": "ecr-login"
}

 

 

 

Share This:

Exit mobile version