Events
Mar 17, 11 p.m. - Mar 21, 11 p.m.
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This tutorial series shows you how to containerize a Python web app and deploy it to Azure Container Apps. A sample web app is containerized, and the Docker image is stored in Azure Container Registry. Azure Container Apps is configured to pull the Docker image from Container Registry and create a container. The sample app connects to Azure Database for PostgreSQL to demonstrate communication between Container Apps and other Azure resources.
There are several options to build and deploy cloud-native and containerized Python web apps on Azure. This tutorial series covers Azure Container Apps. Container Apps is good for running general-purpose containers, especially for applications that span many microservices deployed in containers.
In this tutorial series, you create one container. To deploy a Python web app as a container to Azure App Service, see Containerized Python web app on Azure with MongoDB.
The procedures in this tutorial series guide you to complete these tasks:
In this first part of the series, you learn foundational concepts for deploying a Python web app on Azure Container Apps.
The following diagram shows how you'll use your local environment, GitHub repositories, and Azure services in this tutorial series.
The diagram includes these components:
Azure Container Apps enables you to run microservices and containerized applications on a serverless platform. A serverless platform means that you enjoy the benefits of running containers with minimal configuration. With Azure Container Apps, your applications can dynamically scale based on characteristics such as HTTP traffic, event-driven processing, or CPU or memory load.
Container Apps pulls Docker images from Azure Container Registry. Changes to container images trigger an update to the deployed container. You can also configure GitHub Actions to trigger updates.
Azure Container Registry enables you to work with Docker images in Azure. Because Container Registry is close to your deployments in Azure, you have control over access. You can use your Microsoft Entra groups and permissions to control access to Docker images.
In this tutorial series, the registry source is Azure Container Registry. But you can also use Docker Hub or a private registry with minor modifications.
Azure Database for PostgreSQL:
The sample code stores application data in a PostgreSQL database. The container app connects to PostgreSQL by using a user-assigned managed identity. Connection information is stored in environment variables configured explicitly or through an Azure service connector.
The sample code for this tutorial series is in a GitHub repo that you fork and clone locally. To set up a CI/CD workflow with GitHub Actions, you need a GitHub account.
You can still follow along with this tutorial series without a GitHub account, if you work locally or in Azure Cloud Shell to build the container image from the sample code repo.
To make code changes and push them to a container, you create a new Docker image with your changes. Then, you push the image to Container Registry and create a new revision of the container app.
To automate this process, an optional step in the tutorial series shows you how to build a CI/CD pipeline by using GitHub Actions. The pipeline automatically builds and deploys your code to Container Apps whenever a new commit is pushed to your GitHub repository.
In this tutorial series, you build a Docker container image directly in Azure and deploy it to Azure Container Apps. Container Apps runs in the context of an environment, which is supported by an Azure virtual network. Virtual networks are a fundamental building block for your private network in Azure. Container Apps allows you to expose your container app to the public web by enabling ingress.
To set up CI/CD, you authorize Azure Container Apps as an OAuth app for your GitHub account. As an OAuth app, Container Apps writes a GitHub Actions workflow file to your repo with information about Azure resources and jobs to update them. The workflow updates Azure resources by using the credentials of a Microsoft Entra service principal (or an existing one) with role-based access for Container Apps and a username and password for Azure Container Registry. Credentials are stored securely in your GitHub repo.
Finally, the sample web app in this tutorial series stores data in a PostgreSQL database. The sample code connects to PostgreSQL via a connection string. When the app is running in Azure, it connects to the PostgreSQL database by using a user-assigned managed identity. The code uses DefaultAzureCredential
to dynamically update the password in the connection string with a Microsoft Entra access token during runtime. This mechanism prevents the need to hardcode the password in the connection string or an environment variable, and it provides an extra layer of security.
The tutorial series walks you through creating the managed identity and granting it an appropriate PostgreSQL role and permissions so that it can access and update the database. During the configuration of Container Apps, the tutorial series walks you through configuring the managed identity on the app and setting up environment variables that contain connection information for the database. You can also use an Azure service connector to accomplish the same thing.
To complete this tutorial series, you need:
An Azure account where you can create:
Visual Studio Code or the Azure CLI, depending on what tool you use:
Python packages:
The Python sample app is a restaurant review app that saves restaurant and review data in PostgreSQL. At the end of the tutorial series, you'll have a restaurant review app deployed and running in Azure Container Apps that looks like the following screenshot.
Events
Mar 17, 11 p.m. - Mar 21, 11 p.m.
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Deploy a Quarkus application to Azure Container Apps - Training
Create a Quarkus application, connect it to a PostgreSQL database, and then deploy it to Azure Container Apps.
Certification
Microsoft Certified: Azure Developer Associate - Certifications
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.
Documentation
Build and Deploy a Python Web App with Azure Container Apps - Python on Azure
This tutorial describes how to create a container from a Python web app and deploy it to Azure Container Apps, a serverless platform for hosting containerized applications.
Deploy a containerized Python web app on Azure with MongoDB - Python on Azure
An overview of how to create and deploy a containerized Python web app (Django or Flask) on Azure App Service with MongoDB.
Configure CI/CD for a Python Web App in Azure Container Apps - Python on Azure
Set up CI/CD for a Python web app container in Azure Container Apps by using GitHub Actions triggered on changes (like pull requests) to the main branch of a repo.