Deploying an ASP.NET application to Azure Container Instances (ACI) using Azure DevOps CI/CD pipelines

Deploying an ASP.NET application to Azure Container Instances (ACI) using Azure DevOps CI/CD pipelines involves multiple steps, including containerizing your application, pushing the container image to Azure Container Registry (ACR), and deploying the container to ACI.

The diagram illustrates the process of deploying an application using Azure services. 

  • Developers write the application code and push it to Azure Repos, which is a source code repository in Azure DevOps.
  • Azure Repos manages the source control system (e.g., Git).
  • Azure Pipelines are set up to automate the build and deployment process.
  • The pipeline is triggered whenever there is a code change in the Azure Repo.
  • The pipeline builds the code, runs tests, and creates a container image if needed.
  • Once the build is successful, Azure Pipelines push the containerized application (Docker image) to the Azure Container Registry (ACR).
  • ACR is a private registry used to store and manage container images securely.
  • Azure Pipelines deploy the container image from ACR to Azure Container Instances (ACI).
  • ACI is a platform to run containerized applications in the Azure cloud.
  • The containerized application is hosted in the Azure Container Instance and becomes available to end users over the internet or a private network.
  • Feedback is collected from users to improve the application.
  • Developers analyze the feedback, implement updates or fixes, and push the changes back to the Azure Repo, restarting the cycle.

This workflow represents a continuous integration/continuous deployment (CI/CD) pipeline for containerized applications in Azure.

Here's a detailed guide:


1. Prerequisites

  • An Azure subscription.
  • An ASP.NET application ready for deployment.
  • Docker installed locally for containerization.
  • Azure DevOps project setup with a Git repository for your application code.
  • Access to Azure CLI or Azure Portal for initial setup.

2. Containerize the ASP.NET Application

  1. Add a Dockerfile to Your Application
    Create a Dockerfile in your ASP.NET project directory with the following content (modify based on your framework):

    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    WORKDIR /src
    COPY ["YourProject.csproj", "./"]
    RUN dotnet restore "./YourProject.csproj"
    COPY . .
    WORKDIR "/src"
    RUN dotnet publish "./YourProject.csproj" -c Release -o /app
    
    FROM base AS final
    WORKDIR /app
    COPY --from=build /app .
    ENTRYPOINT ["dotnet", "YourProject.dll"]
  2. Build and Test Locally
    Run the following commands to build and test your container locally:

    docker build -t yourapp:latest .
    docker run -p 8080:80 yourapp:latest
  3. Push Image to Azure Container Registry (ACR)

    • Create an ACR
      az acr create --resource-group <your-resource-group> --name <your-acr-name> --sku Basic
      
    • Log in to ACR
      az acr login --name <your-acr-name>
    • Tag and Push Image
      docker tag yourapp:latest <your-acr-name>.azurecr.io/yourapp:latest
      docker push <your-acr-name>.azurecr.io/yourapp:latest

3. Create Azure DevOps CI/CD Pipeline

CI Pipeline: Build and Push to ACR

  1. Create a Pipeline
    Go to Azure DevOps > Pipelines > Create Pipeline and choose your repository.

  2. Define CI YAML Pipeline
    Add a azure-pipelines.yml file to your repository with the following content:

    trigger:
      branches:
        include:
          - main
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      acrName: '<your-acr-name>'
      imageName: 'yourapp'
    
    steps:
    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '6.x'
    
    - script: |
        docker build -t $(acrName).azurecr.io/$(imageName):$(Build.BuildId) .
        docker push $(acrName).azurecr.io/$(imageName):$(Build.BuildId)
      displayName: 'Build and Push Image'
  3. Run the CI Pipeline
    Trigger the pipeline to build the container image and push it to ACR.


CD Pipeline: Deploy to ACI

  1. Add a Release Pipeline
    Go to Azure DevOps > Pipelines > Releases > New Pipeline.

  2. Define Stages for Deployment

    • Add an Azure CLI task to pull the image from ACR and deploy it to ACI.
    • Sample script for deployment:
      az container create \
        --resource-group <your-resource-group> \
        --name <container-name> \
        --image <your-acr-name>.azurecr.io/yourapp:$(Build.BuildId) \
        --registry-login-server <your-acr-name>.azurecr.io \
        --registry-username <your-acr-username> \
        --registry-password <your-acr-password> \
        --ports 80
  3. Add Continuous Deployment Trigger
    Link the release pipeline to the build pipeline.


4. Verify Deployment

  • Use Azure CLI or Azure Portal to confirm the ACI instance is running.
  • Get the public IP of your ACI instance:
    az container show --resource-group <your-resource-group> --name <container-name> --query ipAddress.ip --output tsv
    
  • Access your ASP.NET application in the browser using the public IP.

5. Automate Secrets Management

Use Azure Key Vault to store sensitive information (e.g., ACR credentials) and integrate it into your Azure DevOps pipelines using the Azure Key Vault task.


This process ensures your ASP.NET application is built, containerized, pushed to ACR, and deployed to ACI in a fully automated CI/CD pipeline using Azure DevOps.

🌟 Master Microsoft Azure with Microsoft Azure in Action! ðŸŒŸ

Dive into the world of cloud computing and supercharge your skills! This practical guide is packed with step-by-step tutorials, real-world use cases, and the latest Azure features to help you build, deploy, and manage scalable cloud apps like a pro. 🚀

🔥 Exclusive Deal Alert! Unlock amazing savings of 34% today! 🎉 Don’t miss this chance to learn Azure while saving big.

👇 Click now to claim your discount and start your Azure journey! ðŸ‘‡
👉 Grab Your 34% Discount Now!

Hurry—this offer won't last forever! ⏳

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example