Introduction
Containerization is the process of packaging an application in an isolated environment called a container. Containers have emerged as a reliable and efficient way of managing software application packaging and deployment across a range of platforms, including the cloud. Azure Container Instances (ACI) is a Microsoft-managed service that enables users to deploy and run containerized applications without having to manage the underlying infrastructure. In this tutorial, we will show you how to manage containers with Azure Container Instances.
Prerequisites
Before we start managing containers with ACI, you will need the following:
- Azure account: You will need an Azure account to create an ACI instance. If you don’t have an Azure account, you can create a free one using the following link: https://azure.microsoft.com/en-us/free/.
- Azure CLI: You will need the Azure Command Line Interface (CLI) installed on your machine to create and manage ACI instances. You can install the Azure CLI by following the instructions provided in this link: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli.
Getting Started
In this tutorial, we will be creating a sample ASP.NET Core application and deploying it to an ACI instance. If you already have a containerized application, you can skip the first section and move on to “Creating an ACI Instance.”
Creating the Sample ASP.NET Core Application
- Open a terminal or command prompt and navigate to the directory where you want to create your ASP.NET Core application. We will be using the
dotnet
command-line tool to create the application. If you do not have thedotnet
tool installed on your machine, you can download it from here. -
Run the following command to create a new ASP.NET Core web application:
dotnet new webApp -o mywebapp
This will create a new ASP.NET Core web application in a directory called
mywebapp
. -
Navigate into the
mywebapp
directory and run the following command to build the application:dotnet build
-
Run the following command to run the application locally:
dotnet run
The application should now be accessible at `http://localhost:5000` in your web browser.
Note: If you are running the application on a different port, the URL will be different. You can check the console output for the correct URL.
- Stop the application by pressing
Ctrl+C
in the terminal.
Containerizing the Sample ASP.NET Core Application
Before we can deploy the application to an ACI instance, we need to containerize it.
- Open a terminal or command prompt and navigate to the root directory of your ASP.NET Core application.
-
Run the following command to create a new Dockerfile:
touch Dockerfile
-
Open the
Dockerfile
in a text editor and add the following content:FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build WORKDIR /src COPY ["mywebapp.csproj", "./"] RUN dotnet restore "./mywebapp.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "mywebapp.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "mywebapp.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "mywebapp.dll"]
This Dockerfile uses the official ASP.NET Core runtime and software development kit (SDK) images provided by Microsoft. It also sets the working directory for the application, exposes the necessary ports, copies the application files into the container, builds and publishes the application, and sets the entry point for the container to run the application.
-
Build the Docker image by running the following command:
docker build -t mywebapp .
This will create a Docker image with the tag
mywebapp
. -
Test the Docker image by running the following command:
docker run -it --rm -p 8080:80 mywebapp
This will run the containerized application and expose it on port 8080. You can access the application by navigating to `http://localhost:8080` in your web browser.
Note: If you are running the Docker container on a different port, the URL will be different. You can check the console output for the correct URL.
- Stop the container by pressing
Ctrl+C
in the terminal.
Creating an AC
In Instance now that we have containerized our application, we can deploy it to an ACI instance.
- Open a terminal or command prompt and log in to your Azure account by running the following command:
az login
This will prompt you to enter your Azure account credentials.
-
Run the following command to create a resource group:
az group create --name myresourcegroup --location eastus
This will create a resource group called
myresourcegroup
in the East US location.Note: You can change the name and location to suit your needs.
- Run the following command to create an ACI instance:
az container create --resource-group myresourcegroup --name mycontainer --image mywebapp --cpu 1 --memory 1 --registry-login-server myregistry --registry-username myusername --registry-password mypassword --ip-address public --ports 80
This will create an ACI instance with the following configurations:
- Resource group:
myresourcegroup
- Name:
mycontainer
- Image:
mywebapp
- CPU: 1
- Memory: 1 GB
- Registry login server:
myregistry
- Registry username:
myusername
- Registry password:
mypassword
- IP address: Public
-
Ports: 80
Note: You will need to replace
myregistry
,myusername
, andmypassword
with your Docker registry login server, username, and password, respectively.
- Run the following command to show the details of the ACI instance:
az container show --resource-group myresourcegroup --name mycontainer
This command will output the details of the ACI instance, including its IP address.
-
Navigate to
http://[ACI IP address]
in your web browser to access the deployed application. The ACI IP address can be found in the output of the previous command.
Scaling an ACI Instance
ACI instances can be easily scaled up or down depending on the load requirements of the application.
- Open a terminal or command prompt and run the following command to scale the ACI instance to 2 CPU cores and 2 GB memory:
az container update --resource-group myresourcegroup --name mycontainer --cpu 2 --memory 2
-
Run the following command to show the details of the ACI instance:
az container show --resource-group myresourcegroup --name mycontainer
This command will output the details of the ACI instance, including its new CPU and memory configurations.
-
To scale the ACI instance back down, repeat step 1 with a lower CPU and memory configuration.
Deleting an ACI Instance
When you no longer need an ACI instance, you can delete it to avoid unnecessary charges.
- Open a terminal or command prompt and run the following command to delete the ACI instance:
az container delete --resource-group myresourcegroup --name mycontainer
-
Run the following command to delete the resource group:
az group delete --name myresourcegroup
This command will delete the resource group and all its resources, including the ACI instance.
Conclusion
Azure Container Instances enables developers to easily manage containers without having to manage the underlying infrastructure. In this tutorial, we covered how to create a sample ASP.NET Core application, containerize the application, create an ACI instance, scale the instance, and delete the instance. ACI is a powerful platform that enables developers to deploy and manage containerized applications with ease. With ACI, developers can focus on building great applications rather than managing infrastructure.