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 guides you through containerizing a Python web app and deploying it to Azure. Azure App Service hosts the single container web app and uses MongoDB for Azure Cosmos DB to store data. With App Service Web App for Containers, you can focus on building and deploying your containers without worrying about managing and maintaining an underlying container orchestrator. When developing web apps, Azure App Service is a good option for taking your first steps with containers. For more information about Azure container options, see Comparing Azure container options.
In this tutorial you:
-
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.
Upon completing this tutorial, you 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. 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 application 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 is utilized for data storage during development.
- MongoDB Connection: Access to the local MongoDB database is provided through a connection string.
The components supporting the Azure environment in this tutorial include:
-
- Web App for Containers in Azure App Service 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 application.
-
-
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, giving you the ability to control over 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 that is utilized in this tutorial for data storage.
-
The containerized application accesses the Azure Cosmos DB resource through a connection string, which is provided as an environment variable.
-
Access to Azure Cosmos DB resource by the containerized application is via a connection string, which is passed as an environment variable to the containerized app.
-
In this tutorial, you build a Docker image, either locally or directly 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'll need the Docker extension and Azure App Service extension.
-
These Python packages:
-
Docker installed locally. This is optional and is only required if you want to run the container locally.
In this tutorial, you build builds a Python restaurant review application that utilizes MongoDB for data storage. For an example using PostgreSQL, see Create and deploy a Flask web app to Azure with a managed identity.
By the end of this tutorial, you’ll have a fully deployed restaurant review app 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