Jenkins pipeline Docker agent with AWS ECR

Docker enhances Jenkins ability to perform various build tasks without having to have the applications or tools installed on the Jenkins server. There are multiple ways Docker agent can be used within Jenkins pipeline. One of the ways are to use Docker images stored in AWS Elastic Container Registry (ECR) in pipeline agent block.

Feb 6, 2023

Docker enhances Jenkins ability to perform various build tasks without having to have the applications or tools installed on the Jenkins server. There are multiple ways Docker agent can be used within Jenkins pipeline. One of the ways are to use Docker images stored in AWS Elastic Container Registry (ECR) in pipeline agent block.

Prereqs

  1. Jenkins server with Docker installed
  2. AWS account with Amazon Elastic Container Registry
  3. Credentials for accessing the registry

Basic Jenkins pipeline structure

pipeline {

  agent any

  stages {
    stage('build') {
      steps {
        sh 'mvn --version'
      }
    }
  }
}

Docker agent block

registryUrl: https://<aws-account-no>.dkr.ecr.us-east-1.amazonaws.com/
image: <ecr-repository-name>/<image-name>:<label>
registryCredentialsId: AWS Credentials

...

agent {
  docker {
    image '0000000000.dkr.ecr.us-east-1.amazonaws.com/mjdk/maven:3.8.7-eclipse-temurin-11'
    registryUrl 'https://0000000000.dkr.ecr.us-east-1.amazonaws.com/'
    registryCredentialsId 'ecr:us-east-1:AWS_ECR_CREDENTIALS_ID'
  }
}

...

Putting it together

pipeline {

  agent {
    docker {
      image '0000000000.dkr.ecr.us-east-1.amazonaws.com/mjdk/maven:3.8.7-eclipse-temurin-11'
      registryUrl 'https://0000000000.dkr.ecr.us-east-1.amazonaws.com/'
      registryCredentialsId 'ecr:us-east-1:AWS_ECR_CREDENTIALS_ID'
    }
  }

  stages {
    stage('build') {
      steps {
        sh 'mvn --version'
      }
    }
  }
}

References