DEV Community

Gias Uddin
Gias Uddin

Posted on

Building and Pushing Docker Images to AWS ECR with GitHub Actions

In modern software development, containers have revolutionized the way applications are deployed and managed. Docker, along with container orchestration tools like Kubernetes, has enabled developers to create consistent and reproducible environments across different stages of the development lifecycle. Amazon Elastic Container Registry (ECR) is a fully-managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images. GitHub Actions, on the other hand, provides powerful automation capabilities for continuous integration and deployment. In this article, we will explore how to leverage GitHub Actions to build Docker images and push them to AWS ECR.

Prerequisites
Before we dive into the implementation, ensure you have the following prerequisites in place:

GitHub Repository: A GitHub repository where your Docker application source code is hosted.

AWS Account: An AWS account with appropriate permissions to create and manage ECR repositories.

Docker Configuration: Docker installed and properly configured on your local development machine.

Setting Up AWS ECR Repository
Before we configure GitHub Actions, let's set up an AWS ECR repository to store our Docker images.

Login to AWS Console: Log in to your AWS Management Console.

Navigate to ECR: Go to the Amazon ECR service from the AWS dashboard.

Create Repository: Click on the "Create repository" button and provide a name for your repository. Note down the repository URI.

Configuring GitHub Actions Workflow
GitHub Actions allow you to define custom workflows using YAML files. These workflows can automate various tasks, including building and pushing Docker images. Here's how you can set up a workflow to build and push Docker images to AWS ECR:

Create Workflow File: In your GitHub repository, create a directory named .github/workflows if it doesn't exist. Inside this directory, create a YAML file (e.g., docker-build-push.yml) to define your workflow.

Define Workflow Steps:

name: Build and Push Docker Image

on:
  push:
    branches:
      - main  # Adjust the branch name as needed

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Repository
      uses: actions/checkout@v2

    - name: Login to AWS ECR
      uses: aws-actions/amazon-ecr-login@v1
      with:
        registry: <your-account-id>.dkr.ecr.<region>.amazonaws.com
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

    - name: Build and Push Docker Image
      env:
        ECR_REGISTRY: <your-account-id>.dkr.ecr.<region>.amazonaws.com
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/repository-name:$IMAGE_TAG .
        docker push $ECR_REGISTRY/repository-name:$IMAGE_TAG
Enter fullscreen mode Exit fullscreen mode

Save to grepper
Replace , , and repository-name with your AWS account ID, the AWS region where your ECR repository is located, and the name of your ECR repository.

AWS Credentials: In your GitHub repository, go to "Settings" > "Secrets" and add two secrets: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. These credentials will be used by GitHub Actions to authenticate with AWS.
Workflow Explanation
The workflow is triggered on each push to the specified branch (in this case, main).

The aws-actions/amazon-ecr-login action is used to authenticate the GitHub Actions runner with AWS ECR using the provided credentials.

The Build and Push Docker Image step performs the following actions:

Builds a Docker image from the repository's Dockerfile.
Tags the image with the ECR repository URI and the Git commit SHA.
Pushes the image to the ECR repository.

Conclusion
GitHub Actions provides a powerful and flexible platform for automating various tasks in your software development lifecycle. By combining GitHub Actions with AWS ECR, you can streamline your Docker image building and deployment process. This allows you to maintain a reliable and efficient continuous integration and continuous deployment (CI/CD) pipeline, ensuring that your applications are deployed with consistency and accuracy. With the steps outlined in this article, you're now equipped to build and push Docker images to AWS ECR seamlessly using GitHub Actions.

Top comments (0)