Azure Container Instances Quickstart: .NET on Linux

This guide walks you through deploying a simple .NET application to Azure Container Instances (ACI) using a Linux container. Azure Container Instances allows you to run containers in Azure without managing virtual machines or more complex orchestration services.

Prerequisites

Step 1: Create a .NET Web Application

First, let's create a basic ASP.NET Core web application. Open your terminal or command prompt and run the following commands:


mkdir AciDotnetApp
cd AciDotnetApp
dotnet new webapp -o WebApp
cd WebApp
        

This creates a new directory named AciDotnetApp, navigates into it, creates a new ASP.NET Core web application project named WebApp, and then changes into the WebApp directory.

Step 2: Dockerize the Application

To deploy to Azure Container Instances, we need to containerize our application. We'll create a Dockerfile in the root of our WebApp project.

Create a new file named Dockerfile in the WebApp directory with the following content:


# Use the official .NET SDK as a builder image
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

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

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

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

# Use a lighter ASP.NET Core runtime image for the final stage
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
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", "WebApp.dll"]
        

Now, build the Docker image. Navigate back to the WebApp directory in your terminal if you aren't already there.


docker build -t mywebapp:latest .
        

This command builds a Docker image and tags it as mywebapp:latest.

Step 3: Log in to Azure and Create a Resource Group

Log in to your Azure account using the Azure CLI:


az login
        

Follow the prompts to authenticate. Then, create a resource group to hold your Azure resources:


az group create --name AciDotnetResourceGroup --location eastus
        

Replace eastus with your preferred Azure region if needed.

Step 4: Deploy to Azure Container Instances

Now, we'll create a container instance using the Docker image we just built. We'll use the Azure CLI's container create command.

First, ensure you are in the WebApp directory where your Dockerfile is located. Then, run the following command:


az container create --resource-group AciDotnetResourceGroup --name mydotnetaci --image mywebapp:latest --dns-name-label mydotnetaciwebsite --ports 80 --location eastus
        

Let's break down this command:

Step 5: Access Your Application

Once the deployment is complete, you can get the FQDN (Fully Qualified Domain Name) of your container instance to access your application.


az container show --resource-group AciDotnetResourceGroup --name mydotnetaci --query "{FQDN:ipAddress.fqdn}" --output tsv
        

Copy the FQDN from the output and paste it into your web browser. You should see your ASP.NET Core application running!

Note: If you encounter any issues, you can check the logs of your container instance using:
az container logs --resource-group AciDotnetResourceGroup --name mydotnetaci

Step 6: Clean Up Resources (Optional)

To avoid ongoing charges, you can delete the resource group and all its resources when you're finished:


az group delete --name AciDotnetResourceGroup --yes --no-wait
        

This command deletes the resource group and everything within it. The --yes flag confirms the deletion, and --no-wait allows the command to return immediately while the deletion proceeds in the background.

Congratulations! You have successfully deployed a .NET application to Azure Container Instances.