| title | Overview of how to deploy a Python web app in Azure Container Apps |
|---|---|
| description | Overview of how to create a container from a Python web app and deploy it to Azure Container Apps, a serverless platform for hosting containerized applications. |
| ms.topic | conceptual |
| ms.date | 08/30/2022 |
| ms.custom | devx-track-python |
| ms.prod | azure-python |
| author | jessmjohnson |
| ms.author | jejohn |
This tutorial shows you how to containerize a Python web app and deploy it to Azure Container Apps. The provided sample web app can be containerized and the container image stored in Azure Container Registry. Azure Container Apps is initially configured to pull the container image from Container Registry. The sample app connects to a Azure Database for PostgreSQL to show communicating between Container Apps and other Azure resources.
There are many options to build and deploy cloud native and containerized Python web apps on Azure. If you're starting off with containers, deploying your web app as a container to either Azure Web App Service or Azure Container Apps is a good first step. This tutorial covers Azure Container Apps. Deploying a Python web app as a container to Azure App Service is covered in the tutorial Containerized Python web app on App Service. Other options such as Azure Container Instance and Azure Kubernetes Service are covered in the article Comparing Container Apps with other Azure container options.
In this tutorial you will:
- Build a Docker container image from a Python web app.
- Configure Azure Container Apps to host the container image.
- Set up a GitHub Action that updates the container image triggered by changes to repo. This last step is optional.
Following this tutorial, you'll have the basis for Continuous Integration (CI) and Continuous Deployment (CD) of a Python web app to Azure.
The service diagram supporting this tutorial shows how your local environment, GitHub repositories, and different Azure services are used in the tutorial.
:::image type="content" source="./media/tutorial-container-apps/service-diagram-overview-for-tutorial-deploy-python-azure-container-apps.png" alt-text="A screenshot of the services using in the Tutorial - Deploy a Python App on Azure Container Apps." lightbox="./media/tutorial-container-apps/service-diagram-overview-for-tutorial-deploy-python-azure-container-apps.png":::
The components supporting this tutorial and shown in the diagram above are:
-
- 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 image from Azure Container Registry. Revisions to container images trigger an update to the deployed container. You can also configure changes to GitHub to trigger the update.
-
- Azure Container Registry enables you to work with Docker images and its components in Azure. It provides a registry that's close to your deployments in Azure and that gives you control over access, making it possible to use your Azure Active Directory groups and permissions.
- In this tutorial, the registry source is Azure Container Registry, but you can also use Docker Hub or a private registry with minor modifications.
-
- The sample code connects to PostgreSQL to storage application data.
- The container app connects to PostgreSQL through environment variables set with Azure Service Connector.
-
- Service Connector helps you connect Azure compute services to other backing services.
- The Service Connector is used during the configuration of Azure Container Apps. The connector generates environment variables containing connection information for PostgreSQL.
-
- The sample code for this tutorial is in a GitHub repo that you can fork and clone locally. To set up a CI/CD workflow with GitHub Actions you'll need a GitHub account.
- You can still follow along with this tutorial without a GitHub account, but you'll have to work in the Azure Cloud Shell to build the container image from the sample code repo.
To make code changes and push them to the container, you create a new container image with the change. 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 shows you how to build a continuous integration and continuous delivery (CI/CD) pipeline with GitHub actions. The pipeline automatically builds and deploys your code to the Container App.
In this tutorial, you'll build a Docker container image directly in Azure and deploy it to Azure Container Apps. Container Apps run in the context of an environment, which is supported by a virtual network (VNET). Azure Virtual Networks (VNet) are 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.
When you initially configure a container in Container Apps, you configure which container image to use from Azure Container Registry. You can create new revisions that use different versions of that image or a new image. Revisions are useful, for example, when you're making code changes or doing A/B testing.
To set up continuous integration and continuous delivery (CI/CD), you connect to a GitHub account, repository, and branch. In addition, you create an Azure Active Directory service principal (or using an existing) context with role-based access control.
The tutorial sample web app uses PostgreSQL to store data. The sample code connects to PostgreSQL via a connection string. The connection string is stored securely using an Azure Service Connector, which helps you connect Azure compute services to other backing services. During the configuration of the Container App, the tutorial walks you through the service connector.
To complete this tutorial, you'll need:
-
An Azure account where you can create:
- Azure Container Registry
- Azure Container App environment
- Azure Database for PostgreSQL
-
Visual Studio Code or Azure CLI, depending on what tool you'll use.
- For Visual Studio Code, you'll need the Container Apps extension.
-
Python packages:
- pyscopg2-binary for connecting to Mongo DB.
- Flask or Django as a web framework.
The Python sample app is a restaurant review app that saves restaurant and review data in PostgreSQL. At the end of the tutorial, you'll have a restaurant review app deployed and running in Azure Container Apps that looks like the screenshot below.
:::image type="content" source="./media/tutorial-container-apps/containerization-of-python-web-app-sample-app-screenshot.png" alt-text="A screenshot of the sample app created from the Python containerized web app used in the Tutorial - Containerized Python App on Azure." lightbox="./media/tutorial-container-apps/containerization-of-python-web-app-sample-app-screenshot.png":::