title | description | ms.topic | ms.date | ms.custom | ms.prod | author | ms.author |
---|---|---|---|---|---|---|---|
Tutorial: Containerized Python web app on Azure |
Overview - Create and deploy a containerized Python web app on Azure |
conceptual |
08/16/2022 |
devx-track-python |
azure-python |
jessmjohnson |
jejohn |
This tutorial shows you 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. App Service Web App for Containers allows you to focus on composing your containers without worrying about managing and maintaining an underlying container orchestrator. When building web apps, Azure App Service is a good option for taking your first steps with containers. For more information about using containers in Azure, see Comparing Azure container options.
In this tutorial you will:
-
Build and run a Docker container locally. This step is optional.
-
Build a Docker container image directly in Azure.
-
Configure an App Service to create a web app based on the Docker container image.
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:
-
-
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 uses a webhook in the registry to get notified of new images. A push of a new image to the repository triggers App Service to pull the image and 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 is passed as an environment variable to the containerized app.
-
In this tutorial, you'll build a Docker image (either locally or directly in Azure) and deploy it to Azure App Service. The App Service pulls the 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 to specify credentials. Specifically, this tutorial uses a system assigned managed identity. Managed identity is configured during setup of App Service to use a registry container image.
The tutorial sample web app 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 an Azure Cosmos DB for MongoDB, you can use the steps for Azure portal, Azure CLI, PowerShell, or VS Code. The sample tutorial requires that you specify a MongoDB connection string, a database name, and a collection name.
-
Visual Studio Code or Azure CLI, depending on what tool you'll use.
- For Visual Studio Code, you'll need the Docker extension and Azure App Service extension.
-
Python packages:
-
Docker installed locally if you want to run container locally.
The Python sample app is a restaurant review app that saves restaurant and review data in MongoDB. For an example of a web app using PostgreSQL, see Deploy a Python web app to Azure with managed identity.
At the end of the tutorial, you'll have a restaurant review app deployed and running in Azure that looks like the screenshot below.
:::image type="content" source="./media/tutorial-container-web-app/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-web-app/containerization-of-python-web-app-sample-app-screenshot.png":::
[!div class="nextstepaction"] Build and test locally