Amazon Elastic Container Registry (ECR) is a fully managed Docker container registry that makes it easy to store, share, and deploy container images. We
will learn how to build docker image for a springboot microservices app
using Azure DevOps(ADO) build pipeline and push docker image into AWS
ECR.
What are we going to do in this lab?
1. Create a Repository in AWS ECR for storing docker images
2. Create an IAM user and AmazonEC2ContainerRegistryFullAccess policy.
3. Create access keys for IAM user in AWS console
4. Create service connection in AzureDevOps to connect to AWS using IAM user
access & secret keys.
5. Create Azure DevOps Build pipeline with below tasks:
maven build for building JAR
build docker image
push docker image into ECR
6. Verify if docker image has been pushed to AWS ECR
Pre-requisites:
Azure DevOps Account setup and project is created in ADO
Hashicorp's Terraform is an open-source tool for provisioning and managing cloud infrastructure. Terraform can provision resources on any cloud platform.
Terraform allows you to create infrastructure in configuration files(tf files) that describe the topology of cloud resources. These resources include virtual machines, storage accounts, and networking interfaces. The Terraform CLI provides a simple mechanism to deploy and version the configuration files to Azure.
Watch the steps in YouTube:
Advantages of using Terraform:
Reduce manual human errors while deploying and managing infrastructure.
Deploys the same template multiple times to create identical development, test, and production environments.
Reduces the cost of development and test environments by creating them on-demand.
How to Authenticate with Azure?
Terraform can authenticate with Azure in many ways, in this example we will use Azure CLI to authenticate with Azure and then we will create resources using Terraform.
The above command will open the browser and will ask your Microsoft account details. Once you logged in, you can see the account info by executing below command:
az account list
Now create a directory to store Terraform files.
mkdir tf-acr
cd tf-acr
Let's create a terraform file to use azure provider. To configure Terraform to use the Default Subscription defined in the Azure CLI, use the below cod.
We will learn how to automate Docker builds using Jenkins. We will use
Python based application. I have already created a repo with source code
+ Dockerfile. We will see how to create Docker image and upload into Azure Container Registry (ACR) successfully.
Pre-requisites:
1. Jenkins is up and running
2. Docker installed on Jenkins instance. Click here to for integrating Docker and Jenkins
3. Docker and Docker pipelines plug-in are installed
4. Create credentials entry for Jenkins for connecting to ACR
5. Repo created in ACR, Click here to know how to do that.
6. port 8096 is opened up in firewall rules.
Step # 1 - Create a pipeline in Jenkins
Name as myACRDockerPipelineJob
Step # 2 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your a should be updated and repo should be updated. pipeline { agent any
environment { //once you create ACR in Azure cloud, use that here registryName = "myakacrregistry/my-python-app" //- update your credentials ID after creating credentials for connecting to ACR registryCredential = 'ACR' dockerImage = '' registryUrl = 'myakacrregistry.azurecr.io' }
Azure Container Registry is a managed, private docker registry service. You can create and maintain Azure
container registries to store and manage your private Docker container
images.
It is alternative to Docker Hub. Azure Container Registry allows you to build, store, and manage
container images and artifacts in a private registry for all types of
container deployments.
Let us see how to upload a docker images from your VM into ACR.
Amazon
ECR uses Amazon S3 for storage to make your container images highly
available and accessible, allowing you to reliably deploy new containers
for your applications. Amazon ECR transfers your container images over
HTTPS and automatically encrypts your images at rest. Amazon ECR is
integrated with Amazon Elastic Container Service (ECS), simplifying your
development to production workflow.
What are we going to do in this lab?
1. Create a Repository in AWS ECR
2. Create an IAM role with AmazonEC2ContainerRegistryFullAccess policy.
3. Assign the role to EC2 instance
4. Download pythonApp from Github.
5. Build docker image for the Python App
6. Tag & push docker image to ECR
7. Run python app in Docker container
Pre-requisites:
Ec2 instance up and running with Docker installed
Make sure you open port 8081
Step 1 - Create a repo in ECR
Go to AWS console and search for ECR
Click on Create Repository
Enter name for your repo - all lower case and Click create repository
Once repo is created, choose the repo and click on view push commands. Note down the account ID
Note the URL from step # 3 below, this will be used for tagging and pushing docker images into ECR.
That's it, you have created repo successfully. Let us create docker images and push it to above repo in ECR.
Step 2- Create an IAM role
You need to create an IAM role with AmazonEC2ContainerRegistryFullAccess policy. Go to AWS console, IAM, click on Roles. create a role
Select AWS services, Click EC2, Click on Next permissions.
Now search for AmazonEC2ContainerRegistryFullAccess policy and click
Skip on create tag.
Now give a role name and create it.
Step 3 - Assign the role to EC2 instance
Go to AWS console, click on EC2, select EC2 instance, Choose instance setting.
Click on Attach/Replace IAM Role
Choose the role you have created from the dropdown.
Select the role and click on Apply.
Now Login to EC2 instance where you have installed Docker. You must be
able to connect to AWS ECR through AWS CLI which can be installed by
sudo apt install awscli -y
Once AWS CLI is installed, you can verify the installation: aws --version
Where your_acct_id is from AWS ECR in the above picture.
You must get a message says Login succeeded. Now let's build a docker
image, I have already created a public repo in Bitbucket. All you need
to do is perform the below command to clone my repo: