Managing containers with Azure Container Instances

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:

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

  1. 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 the dotnet tool installed on your machine, you can download it from here.
  2. 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.

  3. Navigate into the mywebapp directory and run the following command to build the application:

    dotnet build

  4. 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.

  5. 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.

  1. Open a terminal or command prompt and navigate to the root directory of your ASP.NET Core application.
  2. Run the following command to create a new Dockerfile:

    touch Dockerfile

  3. 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.

  4. Build the Docker image by running the following command:

    docker build -t mywebapp .

    This will create a Docker image with the tag mywebapp.

  5. 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.

  6. 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.

  1. 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.

  2. 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.

  3. 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, and mypassword with your Docker registry login server, username, and password, respectively.

  1. 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.

  2. 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.

  1. 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

  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.

  3. 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.

  1. Open a terminal or command prompt and run the following command to delete the ACI instance:

    az container delete --resource-group myresourcegroup --name mycontainer

  2. 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.

Related Post