Docs Menu
Docs Home
/ /

Tutorial: Deploy a Flask App to Azure Container Apps

This tutorial shows you how to containerize a Flask application that uses MongoDB Atlas and deploy it to Azure Container Apps. Azure Container Apps is a serverless platform that simplifies deploying and managing containerized applications without the need to manage infrastructure.

This tutorial shows you how to complete the following tasks:

  • Verify the prerequisites

  • Set up the Flask application

  • Create a Dockerfile

  • Create an Azure Container Registry

  • Build and push the Docker image

  • Create and deploy the Azure Container App

1

Before you begin, complete the following prerequisites:

  • Create a Flask and MongoDB application from the GitHub repository

  • Install Docker Desktop

  • Install Microsoft Azure

  • Install Python 3.9 or later

  • Install Postman Desktop or another API testing tool

This tutorial also requires a MongoDB Atlas account with an active cluster and a Microsoft Azure subscription.

2

Clone the Flask application repository by running the following command:

git clone https://github.com/mongodb-developer/atlas-flask-azure-demo

Using the Atlas UI, create a books collection in a database named bookshelf. Create some documents in the collection, using the following sample documents as a guide:

{
"book": "The Great Gatsby",
"pages": 180,
},
{
"book": "Slaughterhouse-Five",
"pages": 215,
},
{
"book": "If Beale Street Could Talk",
"pages": 197,
}

To start the application, navigate to your working directory and run the following command:

flask run

Use your browser or API testing tool to access the /books endpoint to retrieve the documents that you created.

3

A Dockerfile contains commands to build a Docker image. Create a file named Dockerfile in your working directory, and then add the following content to it:

FROM python:3.9-slim-buster
WORKDIR /azurecontainerappsdemo
COPY ./config/requirements.txt /azurecontainerappsdemo/
RUN pip install -r requirements.txt
COPY . /azurecontainerappsdemo/
ENV FLASK_APP=app.py
EXPOSE 5000
CMD ["flask", "run", "--host=0.0.0.0"]

Move your requirements.txt file to a new folder named config to ensure Docker can locate and copy it during the build process.

This Dockerfile performs the following tasks:

  • Uses python:3.9-slim-buster as the base image

  • Sets the working directory

  • Copies and installs Python dependencies

  • Copies application files

  • Exposes port 5000

  • Configures Flask to accept connections from any network interface

Add the following code to the bottom of your app.py file to ensure Flask runs on all network interfaces:

if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)

You can verify that your Docker image works locally before you deploy to Azure. Build the image by running the following command:

docker build --tag azurecontainerappsdemo .

Then, run the container with your MongoDB connection string by running the following command:

docker run -d -p 5000:5000 -e "CONNECTION_STRING=<MONGODB_ATLAS_URI_STRING_HERE>" azurecontainerappsdemo

Once the container is running, access the application at http://localhost:5000 in your browser.

4

Sign in to the Azure portal and navigate to the Container Registry service. Click Create and specify the following settings for your registry:

  • Resource Group: The name of your resource group

  • Registry Name: Serves as your login URL

  • Location: The location for your registry

Note

You must use the same Resource Group and Location values when you create your Container App.

Click Review and Create.

Once deployment completes, open Visual Studio Code. Ensure that you install the Docker extension.

Navigate to Registries, sign in to your Azure account, and connect your registry.

Log in to your Azure Container Registry by running the following command:

docker login <azure registry url>

Tip

You can find your username and password in the Access Keys section of your Container Registry. Enable Admin Access to view credentials.

Important

If you use Windows, right-click to paste credentials in the terminal to avoid errors.

5

Complete the following steps to build your Docker image and push it to Azure Container Registry. Select the tab that corresponds to your operating system and architecture:

Install Buildx by running the following command:

docker buildx install

Enable Buildx by running the following command:

docker buildx create --use

Build the image for the linux/amd64 platform by running the following command:

docker buildx build --platform linux/amd64 --t <azure registry url>/<image name>:<image tag> --output type=docker .

Push the image to your Azure Container Registry by running the following command:

docker push <azure registry url>/<image name>:<image tag>

Build the image by running the following command:

docker build --t <azure registry url>/<image name>:<image tag> --output type=docker .

Push the image by running the following command:

docker push <azure registry url>/<image name>:<image tag>

Build the image by running the following command:

docker build --t <azure registry url>/<image name>:<image tag> .

Push the image by running the following command:

docker push <image name>:<image tag>

To verify that the push succeeded, navigate to the Container Registry in the Azure portal, then click Repositories to view your image.

6

Navigate to the Container Apps service in the Azure portal and click Create.

In the Basics section, configure the following settings for your Container App:

  • Subscription: Your Azure subscription

  • Resource Group: The same resource group you used for your Container Registry

  • Container App Name

  • Region: The same region you used for your Container Registry

Important

If this is your first Container App, create an environment when you create the app. A Container App environment creates a secure boundary for container apps on the same virtual network.

Configure the following settings in the App Settings section:

  • Uncheck Use quickstart image

  • Select Azure Container Registry as the Image Source

  • Enter your registry, image, and tag information

Add your MongoDB Atlas connection string as an environment variable named CONNECTION_STRING.

Enable ingress and configure the following ingress settings:

  • Ingress traffic: Accepting traffic from anywhere

  • Ingress type: HTTP

  • Transport: Auto

  • Insecure connections: Allowed

  • Target port: 5000

Click Review and Create. After deployment completes, click Go to Resource, then click the Application URL to access your deployed application.

Append /books to the URL to view the books you added previously from your MongoDB Atlas database.

To view the complete code for this tutorial, see the azurecontainerappdemo repository on GitHub.

To learn more about Azure Container Apps, see the Azure Container Apps documentation on the Microsoft website.

Back

Tutorial: Test and Package a Python Library

On this page