title | description | ms.topic | ms.date | ms.custom |
---|---|---|---|---|
Deploy a containerized Python web app on Azure with MongoDB |
An overview of how to create and deploy a containerized Python web app (Django or Flask) on Azure App Service with MongoDB. |
conceptual |
03/17/2025 |
devx-track-python |
This tutorial series shows you how to containerize a Python web app and then either run it locally or deploy it to Azure App Service. App Service Web App for Containers allows you to focus on building your containers without worrying about managing and maintaining an underlying container orchestrator. When you are building web apps, Azure App Service is a good option for taking your first steps with containers. This container web app can use either a local MongoDB instance or MongoDB for Azure Cosmos DB to store data. For more information about using containers in Azure, see Comparing Azure container options.
In this tutorial you:
-
Build and run a Docker container locally. See Build and run a containerized Python web app locally.
-
Build a Docker container image directly in Azure. See Build a containerized Python web app in Azure.
-
Configure an App Service to create a web app based on the Docker container image. See Deploy a containerized Python app to App Service.
After completing the articles in this tutorial series, 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 environment. It highlights the key Azure services used in the development process.
:::image type="content" source="./media/tutorial-container-web-app/containerization-of-python-apps-overview.png" alt-text="A screenshot of the services used in the Tutorial - Containerized Python App on Azure." lightbox="./media/tutorial-container-web-app/containerization-of-python-apps-overview.png":::
The components supporting the developer environment in this tutorial include:
-
Local Development System: A personal computer used for coding, building, and testing the Docker container.
-
Docker Containerization: Docker is employed to package the app and its dependencies into a portable container.
-
Development Tools: Includes a code editor and other necessary tools for software development.
-
Local MongoDB Instance: A local MongoDB database utilized for data storage during development.
-
MongoDB Connection: Access to the local MongoDB database provided through a connection string.
The components supporting the Azure environment in this tutorial include:
-
- In Azure App Service, Web App for Containers uses the Docker container technology to provide container hosting of both built-in images and custom images using Docker.
- Web App for Containers uses a webhook in the Azure Container Registry (ACR) to get notified of new images. When a new image is pushed to the registry, the webhook notification triggers App Service to pull the update and restart the app.
-
-
Azure Container Registry allows you to store and manage Docker images and their components in Azure. It provides a registry located near your deployments in Azure that gives you the ability to control access using your Microsoft Entra groups and permissions.
-
In this tutorial, Azure Container Registry is the registry source, 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 for data storage.
-
The containerized application connects to and accesses the Azure Cosmos DB resource using a connection string, which is stored as an environment variable and provided to the app.
-
In this tutorial, you build a Docker image, either locally or in Azure, and then deploy it to Azure App Service. The App Service pulls the container image from an Azure Container Registry repository.
To securely pull images from the repository, App Service utilizes a system-assigned managed identity. This managed identity grants the web app permissions to interact with other Azure resources, eliminating the need for explicit credentials. For this tutorial, the 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.
To complete this tutorial, you 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, follow the steps in part 2 of this tutorial.
-
Visual Studio Code or Azure CLI, depending on your tool of choice. If you use Visual Studio Code, you need the Docker extension and Azure App Service extension.
-
These Python packages:
- MongoDB Shell (mongosh) for connecting to MongoDB.
- Flask or Django as a web framework.
-
Docker installed locally.
The end result of this tutorial is a restaurant review app, deployed and running in Azure, that looks like the following screenshot.
:::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":::
In this tutorial, you build a Python restaurant review app that utilizes MongoDB for data storage. For an example app using PostgreSQL, see Create and deploy a Flask web app to Azure with a managed identity.
[!div class="nextstepaction"] Build and test locally