Containerize a .NET Application for Azure Container Instances (ACI)

Introduction

This tutorial guides you through the process of containerizing a simple .NET application and deploying it to Azure Container Instances (ACI). Containerization is a key practice for modern application development, enabling consistent deployment across different environments and simplifying management. ACI provides a fast and simple way to run containers in Azure without managing underlying virtual machines.

Prerequisites

Step 1: Create a .NET Web Application

Let's start by creating a basic ASP.NET Core web application. Open your terminal or command prompt and run the following commands:

dotnet new webapp -o MyDotnetApp
cd MyDotnetApp

This creates a new web application project in a directory named MyDotnetApp. You can verify the application by running it locally:

dotnet run

Open your web browser and navigate to http://localhost:5000 (or the port displayed in your terminal) to see your running application.

Step 2: Create a Dockerfile

A Dockerfile is a text file that contains instructions for building a Docker image. Create a file named Dockerfile (no extension) in the root directory of your MyDotnetApp project with the following content:

# Use an official .NET runtime as a parent image
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /app

# Copy the project file and restore dependencies
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the application code
COPY . ./

# Build the application
RUN dotnet publish -c Release -o out

# Use a smaller base image for the final runtime
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app/out .

# Expose the port the application listens on
EXPOSE 80

# Define the command to run the application
ENTRYPOINT ["dotnet", "MyDotnetApp.dll"]

This Dockerfile defines a multi-stage build. The first stage builds the application, and the second stage copies the published output to a smaller runtime image, resulting in a more efficient image.

Step 3: Build the Docker Image

Now, build the Docker image using the Dockerfile. In your terminal, within the MyDotnetApp directory, run:

docker build -t mydotnetapp:latest .

This command builds an image named mydotnetapp with the tag latest.

Step 4: Run the Docker Image Locally

Before deploying to Azure, it's good practice to test your container locally:

docker run -p 8080:80 mydotnetapp:latest

Access your application at http://localhost:8080 in your browser.

Step 5: Tag and Push the Image to Azure Container Registry (ACR)

To deploy to ACI, your container image needs to be accessible, typically from a registry like Azure Container Registry. First, log in to Docker with your Azure credentials:

az acr login --name <your-acr-name>

Replace <your-acr-name> with the name of your Azure Container Registry. If you don't have one, create it using the Azure CLI:

az group create --name MyResourceGroup --location eastus
az acr create --resource-group MyResourceGroup --name <your-acr-name> --sku Basic --admin-enabled true

Now, tag your image for ACR:

docker tag mydotnetapp:latest <your-acr-name>.azurecr.io/mydotnetapp:v1

And push the image:

docker push <your-acr-name>.azurecr.io/mydotnetapp:v1

Step 6: Deploy to Azure Container Instances (ACI)

Finally, deploy your container image to ACI using the Azure CLI. You'll need to enable admin user for your ACR or use a service principal for authentication.

Get your ACR admin credentials:

az acr credential show --resource-group MyResourceGroup --name <your-acr-name> --query "[username, passwords[0].value]" -o tsv

Deploy the container instance:

az container create \
    --resource-group MyResourceGroup \
    --name my-dotnet-aci \
    --image <your-acr-name>.azurecr.io/mydotnetapp:v1 \
    --registry-login-server <your-acr-name>.azurecr.io \
    --registry-username <ACR_USERNAME> \
    --registry-password <ACR_PASSWORD> \
    --dns-name-label mydotnetappdemo \
    --ports 80

Replace <your-acr-name>, <ACR_USERNAME>, and <ACR_PASSWORD> with your actual ACR details.

After the deployment, you can get the public IP address of your container instance:

az container show --resource-group MyResourceGroup --name my-dotnet-aci --query ipAddress.fqdn -o tsv

Navigate to the returned FQDN in your browser to access your deployed .NET application.

Conclusion

Congratulations! You have successfully containerized a .NET application and deployed it to Azure Container Instances. This provides a scalable and manageable way to run your containerized applications in the cloud.