As a software Engineer, I would like to deploy my Dockerized Python Application to Azure Container Instance (ACI) using Azure DevOps so that I can automate my workflow and eliminate any manual intervention during the deployment.
In this blog post, I will walk you through how to build and deploy a Dockerized Python Application to Azure Container Instances (ACI) using Azure DevOps. The workflow will build and deploy the docker image to Azure Container Registry (ACR), and finally, the application will be deployed to Azure Container Instances.
What is Azure? Azure is a cloud computing platform and services, provided by Microsoft. It offers many services, including Databases, storage, Virtual Machines, and more. Azure allows developers and organizations; to build, deploy, and manage applications and services through Microsoft-managed data centers.
Azure Container Instance (ACI) is a managed Azure service that allows you to run containers in the cloud without managing the underlying infrastructure. ACI allows developers to quickly deploy and manage containerized applications without the overhead of managing Kubernetes clusters or virtual machines (infrastructure). ACI is a serverless container service, which simply means that you only pay for the resources (CPU and memory) your container uses and you do not need to worry about how to provision or manage servers.
Azure Container Registry (ACR) is a managed Azure registry service that allows you to manage and store your Docker container images in Azure. It integrates seamlessly with Azure services and it is designed for building and storing Docker container images that can later be used in Azure Container Instance (ACI), Azure Kubernetes Service (AKS), or other containerized services.
Azure DevOps is a set of development tools and services from Microsoft that allow you to build, deploy, test, and deploy your software more efficiently and effectively. It also provides a comprehensive DevOps lifecycle solution, which includes source control, continuous integration and Delivery (CICD), collaboration tools, and project management.
Prerequisites
- Azure Subscription: You must have an active Azure account and subscription. You can create a free account here
- Azure CLI: Install Azure CLI (Command Line Interface) if you do not have it already. Install Azure CLI here
- Docker: Docker is installed on your local machine. If you do not have Docker. Install Docker here
- Azure DevOps Account: Setup an Azure DevOps Organization project here
Below is a step-by-step guide on deploying a Dockerized Python Application from Azure pipelines and later deploying it to an Azure Container instance.
Step 1: Create the Python App with Classes, Getters, and Setters
The Python app will be written in classes and you will be using Flask which is a Python framework to develop the Python application. The app will demonstrate how to manage a simple greeting message and expose getter and setter methods.
The code can be found here
https://medium.com/media/3cf1f346c389775ad3eb29d523aa3d4a/href
Step 2: Explanation of the Python Code
2a: Class Greeting:
- It manages the _message attribute, which stores the greeting message Hello World
- It has the getter method get_message() to fetch the current greeting.
- It has the setter method set_message(message) to modify the greeting message.
2b: Class App:
- It initializes the Flask application and the Greeting class instance.
- The index / route returns the current greeting message.
- The /update/
route allows the greeting message to be updated using either GET or POST method
Step 3: Create the requirements.txt file
Navigate to the root project and create a new file with the filename requirements.txt which will contain the necessary Python dependencies. Copy and paste the code below in the requirements.txt file.
Flask==3.1.0
gunicorn==23.0.0
setuptools>=70.0.0
Step 4: Create the Dockerfile
Navigate to the root project and create a new file with the filename Dockerfile with the code below
The code can be found here
https://medium.com/media/ae63b49fa2f87cbe7ce0090667de9655/href
Step 5: Build the Dockerfile and Test locally
Before deploying using Azure DevOps, let’s test this setup locally
a. Build the Docker image using the command below
docker build -t python-app .
b. Run the Docker container
docker run -p 5000:5000 python-app
c. In your browser, navigate to http://localhost:5000/ to see the greeting message.
d. To update the greeting message from your browser, visit http://localhost:5000/update/
Step 6: Create the Azure DevOps Pipeline
Navigate to the root project and create a new file with the filename azure-pipelines.yml
https://medium.com/media/45a3034f5f4a8d4b0834382115c0982f/href
You also need to configure ServiceConnections.
- connectedServiceNameARM This is a profile for which to establish integration with Azure, which will create permissions and allow you to create services (ACR and ACI on Azure). It will also allow you to link your Azure subscription.
- sampleapp This is the profile for establishing connectivity with ACR (Azure Container Registry)
- ExitoLab, this is the profile for establishing connectivity with Git.

Here is a summary of the YAML file / deployment file
- The pipeline runs automatically when changes are pushed to the main branch
- Different variable definitions define the key configurations, such as azure subscription, resource group, ACR name, image name / tag
- There is a step that verifies if the specified resource group exists, if not it creates it.
- A step checks if the Azure Container Registry (ACR) exist with admin access enabled, if not it creates it.
- Implemented a caching mechanism to make the pipeline (speed up Docker builds) runs faster
- A step installs Trivy, scan the image for High and Critical vulnerabilities but it does not fail that pipeline if a vulnerability is found but it can fail the pipeline if a vulnerability is found. It then pushes the the docker image with tag latest, $(imageTag) to ACR.
- A steps deploys the container image to ACI (Azure Container Instances) with specified resources (CPU, Memory, ports and DNS label)

Once the deployment is successful, you can access the application on http://python-app-demo.eastus.azurecontainer.io:5000/

Conclusion
I hope you can now deploy a Dockerized Python application by leveraging Azure DevOps and Azure Container Instances (ACI) by automating the entire process of building, scanning and deploying with minimal manual effort. This pipeline demonstrates how infrastructure components like resource group, ACR are provisioned if missing, scan docker image using Trivy for vulnerabilities and deploy the Docker image to ACI (Azure Container Instances).
Check out the completed code on GitHub