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.
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.
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
.
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.
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:
--resource-group AciDotnetResourceGroup
: Specifies the resource group where the container instance will be created.--name mydotnetaci
: Assigns a unique name to your container instance.--image mywebapp:latest
: Specifies the Docker image to use.--dns-name-label mydotnetaciwebsite
: Creates a DNS name for your container instance (e.g., mydotnetaciwebsite.eastus.azurecontainer.io
). This must be globally unique.--ports 80
: Opens port 80 on the container instance, allowing HTTP traffic.--location eastus
: Specifies the Azure region for the container instance.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!
az container logs --resource-group AciDotnetResourceGroup --name mydotnetaci
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.