Azure CI/CD for Pushing Docker Image to Registry

Overview 

The Azure CI/CD for pushing Docker images outlines a streamlined process to automate the building and deployment of Docker image using Azure DevOps. This integrated solution ensures efficiency in delivering software applications. 

Prerequisites 

    • Version control system 

    • Azure DevOps Account 

    • Docker 

    • Container Registry 

    • Service connection for registry 

Process 

Step 1: New pipeline 

To create a new pipeline, we can achieve that in different ways. 

Here below content will demonstrate how to do it with YAML file. 

    • Navigate to the “Pipelines” section in the Azure DevOps project 

    • Click on the “New Pipeline” button. This usually appears on the Pipelines dashboard. 

    • Choose the repository where your source code has saved in. This could be Azure Repos, GitHub, Bitbucket, or others. 

    • In my case it is Azure Repos, Choose yours. 

    • Select repo that you want to configure CI/CD. 

    • Then select your build task that meets your application needs. 

    • It creates an .yml file inside a repo that you have chosen, there you can create a new branch and we can push into it. 

NOTE: Please make sure that your chosen branch should have following permissions. 

    • Bypass policies when completing pull requests 

    • Bypass policies when pushing 

    • Force push (rewrite history, delete branches and tags) 

In either way we can achieve that without YAML file 

    • Navigate to the “Pipelines” section in the Azure DevOps project 

    • Click on the “New Pipeline” button. This usually appears on the Pipelines dashboard. 

    • There instead of selecting code source go with use the classic editor option. 

    • Choose the repository where your source code has saved in. This could be Azure Repos, GitHub, Bitbucket, or others. 

    • Select default branch for manual and scheduled builds 

    • Instead of choosing a YAML template, in the Classic Editor, you will start with an empty job. Select “Empty job” to configure the pipeline manually. Or there we can select a template that match for your needs that automatically generates task group for your deployment needs. 

Step 2: Configure pipeline 

After selecting empty job, we should configure a pipeline needs it is other than tasks that involves such as triggers, pipeline variables, Agent pool, Agent specifications etc. 

    • Set a meaningful name for your pipeline in the “Name” field. This helps identify the pipeline in the Azure DevOps interface. 

    • Below that you can select the agent pool where your build and deployment tasks will run. 

    • Agent Pools are groups of build agents, and you can choose the appropriate pool based on your project’s requirements. 

    • You can select a Microsoft-hosted pool, or a self-hosted pool that you manage. 

    • If you don’t have self-hosted pool, just go with Microsoft-hosted pool. 

    • In below Agent specification dropdown, you can see agent list there choose an agent that suits for your case. 

    • In my case I am going with ubuntu-latest. 

    • In agent configuration you can choose a suitable name for your agent. 

    • In agent pool select inherit from pipeline this configuration will use same agent pool that you configured in pipelines. 

Step 3: Create build tasks 

    • In the pipeline configuration, find the section related to agent jobs. It’s typically named “Agent job” or similar. 

    • Click on the “+” icon to add a new task. 

    • A task catalog will appear, displaying various predefined tasks. You can either browse through the categories or use the search bar to find a specific task. 

    • Tasks are available for various purposes such as source code retrieval, build, testing, deployment, etc. 

In my case I have to do a docker build then tag a docker image and push into my container registry location. So, I am going with docker tasks by searching docker. 

    • On top of the add task bar use search option and search as “docker” 

    • From the following result choose Docker task with ‘Build or push Docker images, login, or logout, start or stop containers, or run a docker command’. 

    • After selecting it there have default selection build and push. We can do it build and push in the same task itself, but it is recommended to do it separately. 

Docker Build 

    • In the existing docker task there have a section as command. In that select command as build 

    • This build command is for only building docker image. 

    • There has a section called Dockerfile. There we should configure a location of docker file in our application. If your DockerFile is just inside your repository folder then path should be ‘**/Dockerfile’. 

    • In tag section there we should mention an image tag. 

    • In arguments section use “-t <image name>:<tag>” in the place holder of image name replace your image name and in the place holder of tag replace it by tag. 

Docker Tag 

    • Click on the “+” icon to add a new task.  

    • On top of the add task bar use search option and search as “docker” 

    • From the following result choose Docker task with ‘Build or push Docker images, login, or logout, start or stop containers, or run a docker command’. 

    • There has a section as command. In that select command as Tag 

    • In arguments section use “<image name>:<tag> <registry url>/<image name>:<tag>” in the place holder of image name replace your image name and in the place holder of tag replace it by tag and replace a registry url with your container registry url. 

Registry Login 

    • Click on the “+” icon to add a new task.  

    • On top of the add task bar use search option and search as “docker” 

    • From the following result choose Docker task with ‘Build or push Docker images, login, or logout, start or stop containers, or run a docker command’. 

    • There has a section as command. In that select command as Login. 

    • Choose a container registry as your registry service connection. 

If you haven’t created a service connection before, see below steps to create a service account with your container registry URL and login credentials. 

    • In the lower-left corner there have Project Settings. 

    • In project settings under the “Pipelines” section, select “Service connections.” 

    • Click on the “New service connection” button. 

    • Select the type of service connection as docker registry 

    • There, if you want to push docker image into docker hub choose registry type as docker hub similarly for Azure container registry. 

    • If you have different registry select others to configure it.