Thursday, January 9, 2025

How to upload Docker Images into Azure Container Registry using Azure YAML Pipelines | Automate Docker builds using Azure YAML Pipelines | Upload Docker Image into Azure Container Registry (ACR)

We will learn how to build Docker image and upload the Docker images into Azure Container Registry(ACR) using Azure YAML pipelines.

Pre-requisites:

1. ACR is setup in Azure cloud. (see below for the steps)
2. Already created Azure DevOps dashboard in 
3. Dockerfile created along with the application source code

Create Resource Group

Make sure you are login to Azure portal first.

az login

Execute below command to create a resource group in Azure portal.

az group create --name myResourceGroup --location southcentralus

How to Create Azure Container Registry?

Run the below command to create your own private container registry using Azure Container Registry (ACR).

az acr create --resource-group myResourceGroup --name myacrrepo31 --sku Standard --location southcentralus

You can login to Azure portal to see the ACR repo.

How to create Azure Build YAML Pipeline

1. Login into your Azure DevOps dashboard
2. Click on Pipelines.

3. Click on New Pipeline

4. Click on GitHub Repo as we have code committed into GitHub


5. Enter your repo name and branch name where you have stored your source code along with Dockerfile.
Type python as name and select the below repo


6. Click on Continue. Now choose the template by typing Docker, Select below task and Apply.

 

7. Choose the subscription

Click on Continue

Enter Microsoft credentials.
Now choose ACR repo and enter name for the image and select the path for Dockerfile


Click on Validate and configure.

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- master
resources:
- repo: self

variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'sdsd4'
imageRepository: 'mypythondockerrepo'
containerRegistry: 'myacrrep31.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'

vmImageName: 'ubuntu-latest'

stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)

Now click Save + run and run to start Building the pipeline. Now check the status of the pipeline.



Once the build is completed, you should be able to see the Docker images under 
Services --> Repositories



Please watch the above steps in YouTube Channel:


No comments:

Post a Comment

Setup AquaSec Trivy for Vulnerability scanning | How to Set Up Trivy Scanner in Azure DevOps | How to scan Docker image using Trivy Scanner | Create Azure YAML Pipeline for scanning Docker image

Perform vulnerability scan using Trivy scanner on Azure DevOps Pipeline Pre-requisites: Repo created in ACR,  Click  here to know how to do ...