Tutorial: Deploy a Serverless Container to Azure Container Instances (ACI)

This tutorial guides you through deploying a simple web application as a container to Azure Container Instances (ACI) without the need for a Kubernetes cluster or complex orchestration. ACI is perfect for simple deployments, development, testing, and event-driven scenarios.

Prerequisites

Step 1: Create a Simple Web Application

1
Create a Python Flask App

Create a directory for your project, e.g., aci-flask-app. Inside this directory, create two files:

app.py


from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Azure Container Instances (ACI)!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)
                    

requirements.txt


Flask==2.2.2
                    

Step 2: Containerize the Application

2
Create a Dockerfile

In the same project directory, create a file named Dockerfile:


# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
                    
3
Build and Push the Docker Image

First, build the Docker image:


cd aci-flask-app
docker build -t my-aci-app:v1 .
                    

Next, log in to Azure Container Registry (ACR) or Docker Hub. For this example, we'll use Docker Hub. You'll need to create an account on Docker Hub if you don't have one.


docker login
                    

Tag your image for your Docker Hub username and push it:


docker tag my-aci-app:v1 YOUR_DOCKERHUB_USERNAME/my-aci-app:v1
docker push YOUR_DOCKERHUB_USERNAME/my-aci-app:v1
                    

Replace YOUR_DOCKERHUB_USERNAME with your actual Docker Hub username.

Step 3: Deploy to Azure Container Instances (ACI)

4
Create a Resource Group

Create a resource group to hold your ACI instance:


az group create --name myAciResourceGroup --location eastus
                    
5
Create the Container Instance

Deploy your container using the Azure CLI. This command creates a container group with a single container, exposes port 80, and assigns a public IP address.


az container create --resource-group myAciResourceGroup \
    --name my-aci-container \
    --image YOUR_DOCKERHUB_USERNAME/my-aci-app:v1 \
    --dns-name-label my-aci-app-dns \
    --ports 80 \
    --location eastus
                    

Replace YOUR_DOCKERHUB_USERNAME with your Docker Hub username. The --dns-name-label value must be globally unique.

Step 4: Verify the Deployment

6
Get the Public IP Address

Retrieve the public IP address assigned to your container instance:


az container show --resource-group myAciResourceGroup --name my-aci-container --query ipAddress.fqdn --output tsv
                    

This command will output the fully qualified domain name (FQDN) of your container instance.

7
Access Your Application

Open a web browser and navigate to the FQDN you obtained in the previous step. You should see the message "Hello from Azure Container Instances (ACI)!".

Conclusion

Congratulations! You have successfully deployed a serverless containerized application to Azure Container Instances. ACI provides a quick and easy way to run containers without managing underlying infrastructure.

This tutorial is a basic introduction. For more advanced scenarios, consider using Azure Kubernetes Service (AKS) or other container orchestration platforms.
Explore More Container Tutorials