CI/CD with Jenkins: From GitHub to AWS ECR Using Docker

🚀 My Second DevOps Blog: Application Deploying with Docker, and Pushing to AWS ECR using Jenkins

Hi everyone! 👋
Welcome back to my blog. In my first post, I walked you through the basics of integrating Docker and Jenkins to automate a build process. In this second blog, I'm taking that a step further — building a Docker image, pushing it to AWS Elastic Container Registry (ECR), and running it using Jenkins.

This is a hands-on journey of how I implemented an end-to-end CI/CD pipeline for a static website project. Let’s dive in! 🛠️


🗂️ Tech Stack

  • Jenkins

  • Docker

  • AWS ECR (Elastic Container Registry)

  • GitHub


⚙️ Pipeline Breakdown

Here's the Jenkins pipeline script I used:

groovyCopyEditpipeline {
    agent any

    environment {
        AWS_REGION = 'us-east-1'
        ECR_REPO = 'REPO_NAME'
        IMAGE_TAG = 'new'
    }

    stages {
        stage('Checkout Code') {
            steps {
                git url: 'Github code link with Groovy syntax'
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    sh 'docker build -t $ECR_REPO:$IMAGE_TAG .'
                }
            }
        }

        stage('Login to ECR') {
            steps {
                script {
                    sh 'aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO'
                }
            }
        }

        stage('Push Docker Image') {
            steps {
                script {
                    sh 'docker push $ECR_REPO:$IMAGE_TAG'
                }
            }
        }

        stage('Run Docker Container') {
            steps {
                script {
                    sh 'docker run -d -p 80:80 $ECR_REPO:$IMAGE_TAG'
                }
            }
        }
    }
}

🛠️ Step-by-Step Explanation

1️⃣ Checkout Code

In the first stage, the code is pulled from my GitHub repo. This project is a simple static website built for this Docker-Jenkins demo.

2️⃣ Build Docker Image

Once the code is pulled, Jenkins runs a Docker build command to create an image using the Dockerfile in the repository. The image is tagged using the ECR repo URL and a version tag (new in this case).

3️⃣ Login to ECR

To push images to AWS ECR, Jenkins logs in using AWS CLI and retrieves a temporary login token. This is piped into the Docker login command securely.

4️⃣ Push Docker Image

After logging in, Jenkins pushes the newly built Docker image to the specified ECR repository.

5️⃣ Run Docker Container

As a final validation step, the pushed image is pulled and run locally in a Docker container, mapped to port 80 for quick testing.


📦 AWS ECR Setup Summary

Make sure your ECR repository is created beforehand:

bashCopyEditaws ecr create-repository --repository-name repo --region us-east-1

And ensure Jenkins has access to your AWS credentials, either through environment variables, credentials plugin, or IAM roles (if running Jenkins on EC2).


🔚 Wrapping Up

With this setup, I have a seamless CI/CD pipeline that:

  • Pulls code from GitHub,

  • Builds a Docker image,

  • Pushes it to AWS ECR,

  • And runs the container for testing.

Stage view in Jenkins

This was a great learning experience, especially in integrating Jenkins with AWS and Docker. If you're building container-based projects, this pipeline is a solid starting point.

0
Subscribe to my newsletter

Read articles from Purnachandra Rao Patchigolla directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Purnachandra Rao Patchigolla
Purnachandra Rao Patchigolla