Implement containerized solutions -I

Azure Container Registry (ACR)

Built on top of the open-source Docker Registry 2.0, Azure Container Registry (ACR) is a managed, private Docker registry service. It enables the creation and management of Azure container registries to store and manage your private Docker container images.

Key Features

  • Private Container Registry integrated with Azure AD RBAC.

  • Supports geo-replication (premium tier).

  • Supports storage features like encryption-at-rest for image data security and geo-redundancy for image data protection.

  • Supports integration with Azure Kubernetes Service, Azure App Service, and Azure DevOps for streamlined deployment.

  • Build on demand or automate Image Building using ACR tasks using CI/CD pipelines.

  • Supports image signing using Azure Content Trust for security.

  • Supports include both Windows and Linux images. It also stores Helm charts.

Use Cases

Microservices Deployment Scalable orchestration systems that manage containerized applications like Kubernetes, AKS, Service Fabric, App Service.

ACR Tiers

  • Basic - low image and storage throughput for lower usage scenarios.

  • Standard - increased image and storage throughput ideal for production scenarios.

  • Premium - geo replication, highest storage and concurrent operations.

Azure Container Registry Tasks (ACR Tasks)

ACR Tasks is a robust feature that facilitates automated container image builds, tests, and deployments directly within Azure. It streamlines the CI/CD process for containerized applications by managing builds in response to various triggers.

Task scenarios

  • On-Demand tasks - Build and push a single container image to a container registry on-demand in Azure, without requiring a local Docker Engine installation.

  • Automated tasks - Automatically rebuild images when changes occur in the source code or base images.

  • Multi-step tasks -Support complex build workflows with multi-step tasks defined in YAML.

Examples

  1. Create a managed container registry with the Standard SKU.

     az acr create -n myregistry -g MyResourceGroup --sku Standard
    
  2. Delete an Azure Container Registry.

     az acr delete -n myregistry
    
  3. Imports an image to an Azure Container Registry from another Container Registry.

     az acr import -n myregistry --source sourceregistry.azurecr.io/sourcerepository:sourcetag
    
  4. Queue a local context as a Linux build, tag it, and push it to the registry.

     az acr build -t sample/hello-world:{{.Run.ID}} -r myregistry .
    
  5. Queue a remote GitHub context as a Windows build, tag it, and push it to the registry.

     az acr build -r myregistry https://github.com/Azure/acr-builder.git -f Windows.Dockerfile --platform windows
    
  6. Automate container image builds in the cloud when you commit source code

     az acr task create \
     --registry $ACR_NAME \
     --name taskhelloworld \
     --image helloworld:{{.Run.ID}} \
     --context https://github.com/$GIT_USER/acr-build-helloworld-node.git#master \
     --file Dockerfile \
     --git-access-token $GIT_PAT
    

    This task indicates that whenever code is committed to the main branch in the repository specified by --context, ACR Tasks will build the container image from that branch's code. The Dockerfile specified by --file from the repository root is used to construct the image. The --image argument uses a parameterized value of {{.Run.ID}} for the version part of the image's tag, ensuring the built image corresponds to a specific build and is uniquely tagged.

Explore elements of a Dockerfile

A Dockerfile is a script comprising a series of instructions for building a Docker image. Dockerfiles generally contain the following elements:

  • The base or parent image used to create the new image

  • Commands to update the base operating system and install additional software

  • Build artifacts to include, such as a developed application

  • Services to expose, such as storage and network configurations

  • The command to execute when the container is launched

Create a Dockerfile

The initial step in creating a Dockerfile involves selecting a base image that will serve as the foundation for your application. For instance, if you are developing a .NET application, you may opt for a Microsoft .NET image as your base.

# Use the .NET 6 runtime as a base image
FROM mcr.microsoft.com/dotnet/runtime:6.0

# Set the working directory to /app
WORKDIR /app

# Copy the contents of the published app to the container's /app directory
COPY bin/Release/net6.0/publish/ .

# Expose port 80 to the outside world
EXPOSE 80

# Set the command to run when the container starts
CMD ["dotnet", "MyApp.dll"]

Exercise - Build and run a container image by using Azure Container Registry Tasks

//create resource group
az group create --name az204-acr-rg --location Central India

//create a basic container registry
az acr create --resource-group az204-acr-rg --name myContainerRegistry --sku Basic

//build and push image from a Dockerfile
echo FROM mcr.microsoft.com/hello-world > Dockerfile
//build the image and push to registry
az acr build --image sample/hello-world:v1 --registry myContainerRegistry --file Dockerfile .

//verify the results
az acr repository list --name myContainerRegistry --output table

//list the tags
az acr repository show-tags --name myContainerRegistry --repository sample/hello-world --output table

//Run the sample/hello-world:v1 container image from your container registry
az acr run --registry myContainerRegistry --cmd '$Registry/sample/hello-world:v1' /dev/null

Did you find this article valuable?

Support .NET - Sankarshan Ramesh by becoming a sponsor. Any amount is appreciated!