| title | Tutorial: Containerized Python web apps on Azure |
|---|---|
| description | Overview - Create and deploy a containerized Python web app on Azure |
| ms.topic | conceptual |
| ms.date | 06/27/2022 |
| ms.custom | devx-track-python |
| ms.prod | azure-python |
| author | jess-johnson-msft |
| ms.author | jejohn |
This tutorial shows how to containerize a Python web app and deploy it to Azure. The single container web app is hosted in Azure App Service and uses MongoDB for Azure Cosmos DB to store data. The App Service Web App for Containers allows you to focus on composing your containers without worrying about managing and maintaining an underlying container orchestrator. Use Web App for Containers when you need more control over the runtime, framework, tooling, and packages of a web-based application.
In this tutorial you will:
-
Build a Docker container image from the Python web app code.
-
Run the container locally.
-
Deploy the container image to App Service.
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 two environments (developer environment and Azure) and the different Azure services used in the tutorial.
:::image type="content" source="./media/tutorial-container-web-app/containerization-of-python-apps-overview.png" alt-text="A screenshot of the services using in the Tutorial - Containerized Python App on Azure." lightbox="./media/tutorial-container-web-app/containerization-of-python-apps-overview.png":::
The components supporting this tutorial and shown in the diagram above are:
-
Azure App Service
-
The underlying App Service functionality that enables containerization is Web App for Containers. Azure App Service uses the Docker container technology to host both built-in images and custom images. In this tutorial, you'll build an image from Python code and deploy it to Web App for Containers.
-
Web App for Containers automatically creates a webhook in the registry you selected with the repository as the scope of the trigger event. A push of a new image to the repository triggers an app restart.
-
-
-
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.
-
-
Azure Cosmos DB API for MongoDB
-
The API for MongoDB is a NoSQL database used in this tutorial to store data.
-
Access to Cosmos DB resource is via a connection string, which can be passed as an environment variable to the containerized app.
-
In this tutorial, you'll use Docker locally to build a container image and deploy it to Azure App Service. The App Service pulls a container image from an Azure Container Registry repository.
The App Service uses managed identity to pull images from Azure Container Registry. Managed identity allows you to grant permissions to the web app so that it can access other Azure resources without the need for you to specify credentials. Specifically, this tutorial uses a system assigned managed identity. The managed identity is automatically set up for you when you configure App Service so that publish is from a Docker container.
The tutorial sample web app also uses MongoDB to store data. The sample code connects to Cosmos DB via a connection string.
To complete this tutorial, you'll need:
-
An Azure account where you can create:
- Azure Container Registry
- Azure App Service
- MongoDB for Azure Cosmos DB (or access to equivalent). To create a Azure Cosmos DB for MongoDB, see the instruction for Azure portal, Azure CLI, PowerShell, or VS Code. The sample tutorial requires that you specify a connection string, a database name, and a collection name.
-
Docker installed locally
-
Visual Studio Code or Azure CLI
- For Visual Studio Code with the Docker extension and Azure App Service extension.
-
Python packages:
You can start with sample app in the Django and Flask frameworks, or you can follow along using your own Python app. The sample app is a restaurant review app that saves restaurant and review data in MongoDB. At the end of the tutorial, you'll have a restaurant review app deployed and running in Azure.