Saturday, July 4, 2026

Deploy a Python API to an Azure App service with Azure container registry and Azure DevOps

 Summary : Article explain deployment of phyton api which get data from sql database . On-Prem database in a SQL server instance using the App Services’ Hybrid connection feature.

Objective

This post is to show how to publish a docker container image to an Azure Container registry, then deploy that image to Azure App Service. The web API will require a connection to an On-Prem database in a SQL server instance using the App Services’ Hybrid connection feature. This uses a very simple python API creation calling stored procedures on a SQL server that returns results in a JSON format. The repository can be found here.

Repository: https://github.com/jaya1971/Py-API

Requirements

1. Basic understanding of Azure App Services, Azure DevOps and Docker.

2. An account with Azure cloud and Azure DevOps.

Overview

1. Create an Azure container registry.

2. Configure the Dockerfile with the necessary updates and installs, along with other references to data that will be brought over from the pipeline, through the Dockerfile and into the API.

3. Create a Docker Registry service connection.

4. Create a library group in your Azure DevOps project to host your secret variables and SQL information.

5. Create a pipeline to build and push the image to the Azure Container Registry.

6. Create an App Service in Azure with predefined settings.

7. Pipeline to deploy the CR image to the App Service.

8. Configure pipeline environment variables and pass them from the pipeline to the Python API.

9. Configure the App Service Hybrid connection to interact with an On-Prem SQL server and database.

Automation Order

I would like to point out that I have two pipelines for automating this process. One for deploying an azure container registry and App Service through terraform. The other for the python API. Because the creation and deployment of an Azure container app service must occur in a certain order. I have parameterized my pipelines with Booleans to run certain parts of the automation so I can deploy resources in a specific order.

Here is the order of deployment:

1. Create an Azure Container Registry to hold your docker image (Manual or through Terraform)(I used a terraform module pipeline).

Get Alex Jaya’s stories in your inbox

Join Medium for free to get updates from this writer.

2. Build and push an image to the Azure Container Registry (I ran this from this current repository in a pipeline by setting the “ Update docker image repo” to True).

3. Create a Linux container azure app service (Manual or through Terraform)(I used a terraform module pipeline). When creating a Linux container azure app service, you must reference the image in the container registry. This is why it is done after pushing the image to the container registry.

4. Deploy image to the Linux container app service. (I ran this from this current repository in a pipeline by setting “Deploy img to app service” to True).

Walkthrough

1. I created a main YAML pipeline to call template YAML based on conditions so it would be able to deploy it to different environments. You’ll notice in my repository, that I’m deploying it to dev or prod. This YAML file is what the pipeline uses when triggered. This YAML file is parameterized. The parameters will select a template at runtime.

  • In the main “azure-pipelines.yml” file lines 32–45 contain the variables and library group used to pass variables to the called YAML files.
    - Line 33,34 app: Application name.
    - Line 35,36 repo: The repository created when you pushed the image to the container registry
    - Line 39,40 docker_img_path: is comprised of “<loginServer>/<repository>:<ImageTag>
    - Lines 47–57 contain the references to the called YAML files in the ‘azurepipelines’ folder.
    - Lines 59–71 contain the stages and conditions to determine which YAML file to use.

2. Template YAML files.

  • The called pipelines are broken into two jobs: “Build and Push the Image” and “Deploy Image to App Service”.
  • The first job “BuildAndPushImage” contains three tasks: “printAllVariables@1”, and two “Docker@2”.
    - printAllVariables@1: lists all pipeline variables. This is great for reviewing directory and artifact locations along with variable information. Secret variable values are not displayed.
    - Docker@2: One is to Build the docker image and the other is to push the docker image to the container registry. Variables used are a hybrid of variables declared in the entry YAML template, ADO project library and project service connection.
    - The “arguments” on line 27 contain the variables from the library group that will be used in the python application.
  • The second job “DeployImageToAppService” contains two tasks: “AzureWebContainer@1” and “AzureAppServiceSettings@1”.
    - AzureWebContainer@1: Will deploy the image from the container registry to the app service.
    - AzureAppServiceSettings@1: Contains additional configuration settings.

3. This takes care of the basic setup of having your API’s image in the acr and deployed to your App Service. Since we’re working with an On-Prem database we’ll need to configure a connection that will allow us to access the On-Prem SQL. If you navigate to your App Service’s Log stream (Under Monitoring), you’ll notice an error message that reads something like: “pyodbc.OperationalError: (‘HYT00’, u’[HYT00]……..”. This usually indicates that you do not have a valid connection to your data source, in our case, our On-Prem SQL server. We’ll also have to configure a way to pass the connection string information so our API will properly connect to our data source. The objective is to keep sensitive information hidden. We can do this by creating environment pipeline variables. There are various ways you can do this in Azure DevOps:

  • Environment variables
    - Pipeline variables — make sure you mark sensitive values as secret. Pipeline variables are kept in your pipeline and separate from your repository.
    - Library Group variables — These are ADO’s library groups where you will be able to configure environment variables to be used by your pipelines. The library groups can be secured to all pipelines in your project or to specific ones. You can either place your variables directly in this group or sync it with a keyvault located in your Azure subscription. (Link your keyvault to your ADO Library Group)
  • Once you’ve chosen how you will store your environment variables, you will want to pass these values to your API in your py app. I’ve intentionally named the variables/parameters slightly different to make it easier to distinguish which direction they are being passed. The process is to:
    - Create the reference in your pipeline: This is reference in lines 32–45 in the main YAML pipeline file.
    - Pass those variables in the template YAML files: Arguments on line 27 will pass these variables to the Dockerfile.
    - Pass those values to your Dockerfile as parameters: Dockerfile: Lines 5–12 contain the passed variables from the YAML file as “ARG”. They are converted into environment variables using the ENV tag.
    - In the py app file, you can reference the environment variables passed by the Dockerfile as “os.getenv(‘<ENV_VARIABLENAME>’)

4. Now since we’re connecting to an On-Prem SQL server, you’ll still receive an error message that references a bad connection to your data source and that’s because in this case we are trying to connect to an On-Prem data source. If your data source was an Azure SQL server, you would be able to successfully run your app service API. Let’s configure a hybrid connection to our On-Prem SQL Server. (Great article with step by step — Hybrid Connection)

  • In your Azure app service, navigate to Settings >> Networking >> Hybrid connections >> Add hybrid connection >> Create new hybrid connection. Create your hybrid connection. Your endpoint should be the SQL fqdn that can be found by an On-Prem node where you will install the hybrid connection agent. Copy the “Gateway Connection String”, you’ll need it when you install the agent On-Prem.
  • Download the connection manager to install on an On-Prem node. When creating the connection, select manual and paste the “Gateway Connection String” you copied in the previous step. Restart the “Azure Hybrid Connection Manager Service”. Then your hybrid connection should reflect as:
Press enter or click to view image in full size

Challenges and things learned

There were a few challenges and issues that popped up during this development. One of them being how to pass environmental variables to python, which is different depending on the method being used. The others are listed below.

1. SQL Server instances. We have MS SQL Server configured with instances. When I first configured the hybrid connection, it read successful and connected but I was still receiving the pyodbc connection error message. This was because instances use different ports rather than the default 1433. This can be found in SQL Server Configuration Manager >> SQL Server Network Configuration >> Protocols >> TCP/IP >> IP Addresses tab, under IPAll, you will find the TCP Dynamic Port being used. You’ll want to reference the instance in your ADO Pipeline as “server.domain.com,PORTNUMBER”

2. After everything was up and running, it seemed that the connection to the sql server was still busy with another query and would return an error message that read like: “Error # HY000 [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt)”. I found that setting the MARS (Multiple Active Result Sets) reference in your pyodbc connection string would resolve this issue. This is referenced in Line 38 of the main.py file: “MARS_Connection=YES”

Hope you found this useful!

Saturday, May 2, 2026

Deploying Your Flask Application with Ease: A Step-by-Step Guide Using Microsoft Azure

 Summary: 

This articles works perfectly. simple steps. check in api file to Git. add requirements.txt file . Trigger build using git actions. It works perfectly.

I have created deployment from Azure portal i.e after creating web app I have attached git repo for deployment to this web app. If requirements.txt is good it deploys flash api to azure web app without any issues.

Deploying applications to the cloud requires choosing suitable services and understanding the deployment process. In this tutorial, we’ll focus on deploying a Python-based Flask application to Azure using Azure App Service.

Step 1: Setting Up Your Flask Application

  1. Create Your Flask Application: Start by building your Flask application. Ensure that the main file is named application.py for compatibility during deployment on Azure. This is crucial for maintaining data integrity in a cloud infrastructure.
  2. Go to your Github and change the name of adding another file with application.py Note: You might get errors when you have more than one file with py extension.
Press enter or click to view image in full size

Step 2: Azure Portal Setup

  1. Sign In/Sign Up on Azure Portal: https://portal.azure.com/

If you haven’t already, sign up for an Azure account. You can opt for a student subscription or use the free subscription for the first year.

2. Accessing App Services:

Navigate to the Azure portal and locate “App Services.” and click on “Create” to start the process of creating a new service.

Press enter or click to view image in full size

Step 3: Confugure your Web App

  1. Choose “Web App” among the options.

2. Select your subscription and create a new resource group (if needed).

3. Fill in the instance name, select the “code” option, choose the Python version, and leave other settings as default.

Get Johnas Chami’s stories in your inbox

Join Medium for free to get updates from this writer.

4. Proceed to the “databases” section and maintain default settings and then skip ahead to the “deployments” section.

Press enter or click to view image in full size

Step 4: Confuguring GitHub Actions

  1. Enable GitHub Actions settings and link your GitHub account.

2. Choose the repository and branch for deployment and skip other settings, review your configurations, and proceed with the deployment creation.

3. After deployment, a GitHub workflow file will be generated in your repository.

Step 5: Checking Deployment Status

  1. Access the created resource and note the default domain address.

2. Give some time for the deployment to complete as the model is still in the deployment phase.

Press enter or click to view image in full size

3. Monitor your deployment with GitHub Actions. Go to your GitHub repository and navigate to GitHub Actions and observe the step-by-step progress of your application’s deployment.

Press enter or click to view image in full size

Step 6: Verifying Successful Deployment

  1. Check GitHub Actions to ensure both build and deployment phases are successful.

2. Note the time taken for the deployment process.

Final Step:

  1. Accessing the deployed application: Use the default domain provided to access your deployed application.
Press enter or click to view image in full size
  1. Verify that your Flask application is running as expected.
Press enter or click to view image in full size

Congratulations! You’ve successfully deployed your Flask application to Azure using Azure App Service. This process leverages GitHub Actions for smooth integration and provides a reliable cloud environment for your Python-based ML prediction model.

Wednesday, April 29, 2026

Build and Deploy Dockerize Python Application to Azure Container Instances (ACI) using Azure DevOps

 

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/<message> 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/<new-message>. The actual greeting message will be http://localhost:5000/update/Hello%20from%20Dockerized%20App

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

Deploy a Python API to an Azure App service with Azure container registry and Azure DevOps

 S ummary : Article explain deployment of phyton api which get data from sql database . On-Prem database in a SQL server instance using the...