Voiced by Amazon Polly |
Overview
Adopting a robust DevOps pipeline in today’s fast-paced development environment is crucial for delivering high-quality software quickly.
In this guide, we will set up a continuous integration and deployment (CI/CD) pipeline using AWS CodeBuild to build a Docker image and push it to the Amazon Elastic Container Registry (ECR).
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
In this blog, we will go through the end-to-end process of building and pushing Docker images using AWS DevOps tools, empowering developers to streamline containerized application development, deployment, and management on Amazon Web Services.
Prerequisites
Before diving into the blog, ensure you have the following prerequisites:
- An AWS account with the necessary permissions to create and manage AWS CodeBuild projects, Amazon ECR repositories, and AWS IAM roles.
- AWS Command Line Interface (CLI) is installed and configured with your credentials.
Step-by-Step Guide
Step 1: Create an Amazon ECR Repository
Start by creating an Amazon ECR repository to store your Docker images. This can be done through the AWS Management Console or the AWS CLI.
AWS CLI Command:
1 |
$ aws ecr create-repository --repository-name your-repo-name |
Step 2: Set up the AWS CodeBuild Project
- Navigate to the AWS CodeBuild console.
- Click on “Create build project.”
- Configure the build project.
- Project name: Choose a name for your project.
- Source provider: Select the source provider (GitHub, Bitbucket, etc.) and connect your repository.
- Environment image: Choose a managed image (e.g., aws/codebuild/standard:5.0) with Docker support.
- Buildspec: Create a yml file in your source repository to define build steps.
Example buildspec.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
yamlCopy code version: 0.2 phases: install: commands: - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 & - timeout 15 sh -c "until docker info; do echo .; sleep 1; done" pre_build: commands: - echo Logging in to Amazon ECR... - aws --version - AWS_DEFAULT_REGION=ap-south-1 - echo $AWS_DEFAULT_REGION - aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin ***********.dkr.ecr.ap-south-1.amazonaws.com - REPOSITORY_URI=*********.dkr.ecr.ap-south-1.amazonaws.com/cicd build: commands: - echo Build started on `date` - docker images - docker build -t testimage . - echo $REPOSITORY_URI - echo $REPOSITORY_URI:testimage - docker tag testimage:latest $REPOSITORY_URI:testimage post_build: commands: - echo Build completed on `date` - echo Pushing the Docker image... - docker push $REPOSITORY_URI:testimage - echo Writing image definition file... - echo Done artifacts: files: imagedefinitions.json |
- Define environment variables:
- AWS_DEFAULT_REGION: Set to your AWS region.
- ECR_REPO_URL: Set to your ECR repository URL.
Step 3: Grant Permissions
Ensure the CodeBuild service role has the necessary permissions to interact with Amazon ECR. Attach the AmazonEC2ContainerRegistryPowerUser policy to the CodeBuild service role.
Step 4: Trigger the Build
Trigger the CodeBuild project manually or set up a webhook to trigger builds automatically on code changes.
You will get the output below once the Build and push are completed.
Conclusion
Congratulations! You’ve successfully set up a CI/CD pipeline for building and deploying Docker images with AWS DevOps. This pipeline allows you to automate building Docker images, pushing them to Amazon ECR, and integrating seamlessly with your development workflow. As you continue to enhance your application, consider extending this pipeline to include additional stages, such as testing and deployment to Amazon ECS or Kubernetes clusters. This scalable and efficient DevOps pipeline empowers teams to deliver software confidently and quickly.
Drop a query if you have any questions regarding Amazon ECR and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, Microsoft Gold Partner, and many more, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. What is Amazon ECR, and why should I use it for storing Docker images?
ANS: – Amazon ECR is a fully managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images. It integrates seamlessly with other AWS services, providing a secure and scalable solution for storing and retrieving Docker images. Using Amazon ECR ensures reliable access to container images during the deployment process.
2. Why choose AWS CodeBuild for building and pushing Docker images?
ANS: – AWS CodeBuild is a fully managed continuous integration service that scales automatically and eliminates the need to manage and build servers. It supports building Docker images directly, and when combined with Amazon ECR, it provides a streamlined and efficient process for building, testing, and deploying Dockerized applications.
3. How do I configure AWS CodeBuild to build and push Docker images into Amazon ECR?
ANS: – The configuration involves setting up a buildspec file in your source code repository. This file defines the build steps, including Docker image creation and pushing to Amazon ECR. AWS CodeBuild integrates with your source code repository and executes these steps based on triggers, such as code commits.
WRITTEN BY Karthik Kumar P V
Karthik Kumar Patro Voona is a Research Associate (Kubernetes) at CloudThat Technologies. He Holds Bachelor's degree in Information and Technology and has good programming knowledge of Python. He has experience in both AWS and Azure. He has a passion for Cloud-computing and DevOps. He has good working experience in Kubernetes and DevOps Tools like Terraform, Ansible, and Jenkins. He is a very good Team player, Adaptive and interested in exploring new technologies.
Click to Comment