In this tutorial, we will explore Azure Container Instances (ACIs) and learn how to deploy and manage them. ACIs are a service provided by Microsoft Azure that allows you to run a container without worrying about the underlying infrastructure. You can deploy individual containers or entire applications in just a few seconds.
Prerequisites
Before we begin, ensure that you have the following prerequisites:
- An Azure subscription
- Azure CLI installed
- Docker installed
What is a Container?
A container is a lightweight, standalone executable package that includes everything required to run an application, including code, runtime, system tools, libraries, and settings. Containers enable you to package an application or service and its dependencies into a single, self-contained unit that can run consistently across different environments.
What is Azure Container Instances?
Azure Container Instances (ACI) is a serverless container service provided by Microsoft Azure. This service enables the deployment of Docker containers without the need for virtual machines, cluster management, or orchestration tools. It provides fast and efficient container startup with automatic scaling, perfect for running microservices, database backups, and batch processing jobs.
Creating a Container Image
To deploy a container instance, we first need to create a container image. In this example, we will create a simple Node.js application that returns “Hello World” when accessed through a web browser.
- Create a new directory called
hello-world
.
mkdir hello-world
- Navigate to the new directory.
cd hello-world
- Create a new file called
app.js
and add the following code.
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello Worldn');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
- Create a new file named
Dockerfile
and add the following code.
# Use an official Node.js runtime as a parent image
FROM node:12
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in package.json
RUN npm install
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Define the command to run the app
CMD ["npm", "start"]
The Dockerfile specifies how to build the container image and includes all of the instructions needed to create a self-contained package.
- Run the following command to build the image.
docker build -t hello-world-image .
This command uses the Dockerfile to build a new image named hello-world-image
in the current directory (.
). It takes some time to build the image, depending on your internet connection.
- Check that the image was created successfully by running the following command.
docker images
This command should return a list of all the images on your machine.
Deploying a Container Instance
Now that we have created a container image, we can use Azure Container Instances to deploy it.
- Make sure you have logged into Azure using the following command.
az login
- Create a new resource group, using the following command.
az group create --name hello-world-group --location eastus
- Deploy the container using the following command.
az container create --resource-group hello-world-group
--name hello-world-container --image hello-world-image
--cpu 1 --memory 1
--ports 3000
--ip-address public
--location eastus
This command will create a new container instance named hello-world-container
using the image we just created. The --cpu
and --memory
flags define the resources the container will use. The --ports
flag specifies the ports the application will listen on. The --ip-address
flag specifies that the container will have a public IP address.
- Check the status of the container instance using the following command.
az container show --resource-group hello-world-group
--name hello-world-container --query instanceView.state
This command will return the state of the container. If successful, the state should be “running”.
- Access the container instance using the URL provided in the command output.
kubectl get service hello-world
This command will return the external IP address of the container instance.
- Navigate to the URL in a web browser. You should see “Hello, world!” displayed on the page.
Congratulations! You have deployed a container instance with Azure Container Instances.
Managing Container Instances
Now that we have deployed a container instance, it’s essential to understand how to manage and monitor it.
Restarting a Container Instance
If a container instance crashes or becomes unresponsive, we can restart it to restore normal operation.
- Retrieve the name of the container instance.
az container show --resource-group hello-world-group
--name hello-world-container --query name
- Restart the container instance using the following command.
az container restart --resource-group hello-world-group
--name hello-world-container
Scaling a Container Instance
Azure Container Instances offers horizontal scaling by allowing containers to be replicated. This enables us to increase the capacity of our application by adding more containers.
- Retrieve the name of the container instance.
az container show --resource-group hello-world-group
--name hello-world-container --query name
- Scale the container instance using the following command.
az container update --resource-group hello-world-group
--name hello-world-container --cpu 2 --memory 2
This command will update the container instance with more resources.
Deleting a Container Instance
When we no longer need a container instance, it’s essential to delete it to avoid incurring unnecessary costs.
- Delete the container instance using the following command.
az container delete --resource-group hello-world-group
--name hello-world-container
This command will delete the container instance, and all its associated resources, from Azure.
Conclusion
In this tutorial, we learned how to create a container image, deploy it using Azure Container Instances, and manage it. You are now familiar with the power of Azure Container Instances and how they can be used for fast and efficient container deployment.