NashTech Blog

Table of Contents

Introduction

Tagging Docker images correctly is crucial for maintaining clear and efficient workflows in your containerized applications. In this blog post, we’ll explore how to tag Docker images with the latest tag and push them to a Docker registry, such as Azure Container Registry (ACR), Docker Hub, or any other registry. We’ll also cover how to automate this process using a CI/CD pipeline in Azure DevOps.

Understanding Docker Image Tagging

Docker tags are labels attached to Docker images to identify specific versions or variants of the image. Tags are useful for:

  • Differentiating between versions of an image (e.g., v1.0.0, v2.0.0).
  • Marking images with metadata such as build numbers or timestamps.
  • Indicating the “latest” or default version of an image with the latest tag.

The latest tag is a convention used to signify the most recent or preferred version of an image. However, it’s important to note that latest does not automatically update; it must be explicitly set.

Prerequisites

Before we start, ensure you have the following:

  • Docker installed on your local machine.
  • An account on a Docker registry (e.g., Docker Hub, Azure Container Registry).
  • Basic knowledge of Docker commands and Dockerfile syntax.

Tagging Docker Images Locally

First, let’s look at how to manually tag a Docker image with the latest tag.

Step 1: Build Your Docker Image

Navigate to your project directory containing the Dockerfile and run the following command to build the Docker image:

docker build -t my-app:1.0.0 .

Here, my-app is the name of the image and 1.0.0 is the version tag.

Step 2: Tag the Image with the Latest Tag

After building the image, you can tag it with the latest tag:

docker tag my-app:1.0.0 my-app:latest

This command does not create a new image; it simply creates a new tag for the existing image.

Step 3: Push the Tagged Image to a Docker Registry

Finally, push both tags to your Docker registry:

docker push my-app:1.0.0
docker push my-app:latest

Replace my-app with the appropriate name for your Docker image.

Automating the Process with Azure DevOps

Manually tagging and pushing Docker images can be error-prone and inefficient. Let’s automate this process using Azure DevOps.

Step 1: Set Up Your Azure DevOps Project

  1. Go to Azure DevOps and create a new project.
  2. Create a new repository or use an existing one to store your application code and Dockerfile.

Step 2: Define the Azure DevOps Pipeline

Create a file named azure-pipelines.yml in the root of your repository and add the following content:

trigger:
- main  # or the branch you want to trigger the build on

variables:
  imageName: your-image-name  # Replace with your image name
  acrName: your-acr-name  # Replace with your ACR name
  buildId: $(Build.BuildId)

stages:
- stage: BuildAndPush
  displayName: Build and Push Docker Image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - task: DockerInstaller@0
      displayName: Install Docker

    - task: AzureCLI@2
      inputs:
        azureSubscription: 'YourServiceConnection'  # Replace with your service connection name
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          az acr login --name $(acrName)

    - script: |
        docker build -t $(acrName).azurecr.io/$(imageName):$(buildId) .
        docker tag $(acrName).azurecr.io/$(imageName):$(buildId) $(acrName).azurecr.io/$(imageName):latest
      displayName: Build Docker Image

    - script: |
        docker push $(acrName).azurecr.io/$(imageName):$(buildId)
        docker push $(acrName).azurecr.io/$(imageName):latest
      displayName: Push Docker Image

Explanation

  • Trigger: The pipeline triggers on changes to the main branch.
  • Variables: Define the Docker image name, ACR name, and build ID.
  • Stages and Jobs: The pipeline has a single stage BuildAndPush with one job Build.
  • Steps:
  • Install Docker: Installs Docker on the build agent.
  • Login to ACR: Logs in to the Azure Container Registry using the Azure CLI.
  • Build Docker Image: Builds the Docker image and tags it with both the build number and latest.
  • Push Docker Image: Pushes both tags to the ACR.

Step 3: Set Up Azure DevOps Service Connection

  1. Go to your Azure DevOps project settings.
  2. Under “Pipelines”, click on “Service connections”.
  3. Click on “New service connection”, choose “Docker Registry”, and select “Azure Container Registry”.
  4. Name the service connection (e.g., YourServiceConnection).

Step 4: Commit and Run the Pipeline

  1. Commit the azure-pipelines.yml file to your repository and push the changes to the main branch.
  2. Go to Azure DevOps, navigate to Pipelines, and run the pipeline.

Conclusion

Tagging Docker images with the latest tag and automating the process with Azure DevOps ensures consistency and efficiency in your CI/CD workflows. By following the steps outlined in this guide, you can seamlessly build, tag, and push Docker images to your preferred registry. This automation minimizes manual errors and speeds up the deployment process, enabling a smoother and more reliable application delivery pipeline.

Picture of Pradeep

Pradeep

Leave a Comment

Suggested Article

Discover more from NashTech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading