Overview: Containerized Python web app on Azure with MongoDB
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.
Service overview
The service diagram supporting this tutorial shows two environments (developer environment and Azure) and the different Azure services used in the tutorial.
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 Microsoft Entra 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 Azure Cosmos DB for MongoDB is a NoSQL database used in this tutorial to store data.
Access to Azure Cosmos DB resource is via a connection string, which is passed as an environment variable to the containerized app.
Authentication
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 Azure Cosmos DB via a connection string.
Prerequisites
To complete this tutorial, you'll need:
An Azure account where you can create:
- Azure Container Registry
- Azure App Service
- Azure Cosmos DB for MongoDB (or access to an equivalent). To create an Azure Cosmos DB for MongoDB database, we recommend you follow the steps in part 2 of this tutorial.
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.
Sample app
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 Create and deploy a Flask web app to Azure with a 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.