Automate Docker Image Deployment to AWS ECR Using GitHub Actions[2024]

S3CloudHub - Sep 5 - - Dev Community

Introduction
As containerization becomes the standard for deploying applications, Docker provides a consistent and efficient way to package software. AWS ECR serves as a managed container image registry that simplifies storing, managing, and deploying Docker container images. GitHub Actions, a continuous integration and continuous delivery (CI/CD) platform, enables automation of workflows directly from your GitHub repository. Combining these tools can significantly enhance your development and deployment processes.

Prerequisites
Before we dive into the automation process, ensure you have the following prerequisites:

  1. Docker: Installed and configured on your local machine.
  2. AWS Account: Access to AWS services with appropriate permissions.
  3. AWS CLI: Installed and configured with your AWS credentials.
  4. GitHub Repository: A repository where your Dockerfile and source code reside.
  5. AWS ECR Repository: Created to store your Docker images.

Step 1: Create an AWS ECR Repository

  1. Log in to AWS Management Console: Navigate to the ECR service.
  2. Create Repository:
  • Click on “Create repository.”
  • Choose a repository name and settings according to your requirements.
  • Click “Create repository” to finalize.

Step 2: Set Up AWS Credentials in GitHub

  1. Generate AWS Access Keys:
  • Log in to the AWS Management Console.
  • Navigate to IAM (Identity and Access Management) and create a new user with programmatic access.
  • Attach policies such as AmazonEC2ContainerRegistryFullAccess to the user.
  • Save the access key ID and secret access key.
  1. Add Secrets to GitHub Repository:
  • Go to your GitHub repository’s settings.
  • Navigate to “Secrets and variables” and select “Actions.”
  • Add the following secrets:
  • AWS_ACCESS_KEY_ID: Your AWS access key ID.
  • AWS_SECRET_ACCESS_KEY: Your AWS secret access key.
  • AWS_REGION: The AWS region where your ECR repository is located.
  • ECR_REPOSITORY: The name of your ECR repository.

Step 3: Create GitHub Actions Workflow

  1. Create Workflow File:
  • In your GitHub repository, navigate to .github/workflows (create this directory if it doesn’t exist).
  • Create a file named deploy-to-ecr.yml.
  1. Define Workflow Configuration: Add the following YAML configuration to the deploy-to-ecr.yml file:
name: Build and Deploy Docker Image to AWS ECR

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2

    - name: Log in to AWS ECR
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build Docker image
      run: docker build -t ${{ secrets.ECR_REPOSITORY }} .

    - name: Tag Docker image
      run: |
        docker tag ${{ secrets.ECR_REPOSITORY }}:latest ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/${{ secrets.ECR_REPOSITORY }}:latest

    - name: Push Docker image to AWS ECR
      run: |
        docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/${{ secrets.ECR_REPOSITORY }}:latest

Enter fullscreen mode Exit fullscreen mode

Replace ${ { secrets.AWS_ACCOUNT_ID } } with your AWS account ID. This workflow will automatically build and deploy your Docker image to ECR when changes are pushed to the main branch.

Step 4: Test the Automation

  1. Commit and Push Changes:
  • Commit the deploy-to-ecr.yml file and push it to your GitHub repository.
  1. Monitor Workflow Execution:
  • Navigate to the “Actions” tab in your GitHub repository to monitor the workflow execution.
  • Ensure that the Docker image builds successfully and is pushed to your ECR repository.

Conclusion
By following these steps, you’ve successfully set up an automated pipeline for deploying Docker images to AWS ECR using GitHub Actions. This automation not only enhances your deployment efficiency but also integrates seamlessly with your existing development workflow.

If you encounter any issues or need further customization, refer to the GitHub Actions documentation and the AWS ECR documentation for additional guidance.

Feel free to leave your comments or questions below. Happy deploying!

Explore more detailed content and step-by-step guides on our YouTube channel:-
image alt text here

🔗 GitHub Repository: https://github.com/S3CloudHubRepo/packer-ecr-docker

Connect with Us!
Stay connected with us for the latest updates, tutorials, and exclusive content:

WhatsApp:- https://www.whatsapp.com/channel/0029VaeX6b73GJOuCyYRik0i
facebook:- https://www.facebook.com/S3CloudHub
youtube:- https://www.youtube.com/@s3cloudhub
github:- https://github.com/S3CloudHubRepo

Connect with us today and enhance your learning journey!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player