Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Deploy a Flask or FastAPI web app on Azure Container Apps

  • 2024-12-16
Feedback

In this article

This tutorial shows you how to containerize a PythonFlask orFastAPI web app and deploy it toAzure Container Apps. Azure Container Apps usesDocker container technology to host both built-in images and custom images. For more information about using containers in Azure, seeComparing Azure container options.

In this tutorial, you use theDocker CLI and theAzure CLI to create a Docker image and deploy it to Azure Container Apps. You can also deploy withVisual Studio Code and theAzure Tools Extension.

Prerequisites

To complete this tutorial, you need:

Get the sample code

In your local environment, get the code.

git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart.git

Add Dockerfile and .dockerignore files

Add aDockerfile to instruct Docker how to build the image. TheDockerfile specifies the use ofGunicorn, a production-level web server that forwards web requests to the Flask and FastAPI frameworks. The ENTRYPOINT and CMD commands instruct Gunicorn to handle requests for the app object.

# syntax=docker/dockerfile:1FROM python:3.11WORKDIR /codeCOPY requirements.txt .RUN pip3 install -r requirements.txtCOPY . .EXPOSE 50505ENTRYPOINT ["gunicorn", "app:app"]

50505 is used for the container port (internal) in this example, but you can use any free port.

Check therequirements.txt file to make sure it containsgunicorn.

Flask==3.1.0gunicorn

Configure gunicorn

Gunicorn can be configured with agunicorn.conf.py file. When thegunicorn.conf.py file is located in the same directory wheregunicorn is run, you don't need to specify its location in theENTRYPOINT orCMD instruction of theDockerfile. For more information about specifying the configuration file, seeGunicorn settings.

In this tutorial, the suggested configuration file configures GUnicorn to increase its number of workers based on the number of CPU cores available. For more information aboutgunicorn.conf.py file settings, seeGunicorn configuration.

# Gunicorn configuration fileimport multiprocessingmax_requests = 1000max_requests_jitter = 50log_file = "-"bind = "0.0.0.0:50505"workers = (multiprocessing.cpu_count() * 2) + 1threads = workerstimeout = 120

Add a.dockerignore file to exclude unnecessary files from the image.

.git***/*.pyc.venv/

Build and run the image locally

Build the image locally.

docker build --tag flask-demo .

Run the image locally in a Docker container.

docker run --detach --publish 5000:50505 flask-demo

Open thehttp://localhost:5000 URL in your browser to see the web app running locally.

The--detach option runs the container in the background. The--publish option maps the container port to a port on the host. The host port (external) is first in the pair, and the container port (internal) is second. For more information, seeDocker run reference.

Deploy web app to Azure

To deploy the Docker image to Azure Container Apps, use theaz containerapp up command. (The following commands are shown for the Bash shell. Change the continuation character (\) as appropriate for other shells.)

az containerapp up \  --resource-group web-flask-aca-rg --name web-aca-app \  --ingress external --target-port 50505 --source .

When deployment completes, you have a resource group with the following resources inside of it:

  • An Azure Container Registry
  • A Container Apps Environment
  • A Container App running the web app image
  • A Log Analytics workspace

The URL for the deployed app is in the output of theaz containerapp up command. Open the URL in your browser to see the web app running in Azure. The form of the URL will look like the followinghttps://web-aca-app.<generated-text>.<location-info>.azurecontainerapps.io, where the<generated-text> and<location-info> are unique to your deployment.

Make updates and redeploy

After you make code updates, you can run the previousaz containerapp up command again, which rebuilds the image and redeploys it to Azure Container Apps. Running the command again takes in account that the resource group and app already exist, and updates just the container app.

In more complex update scenarios, you can redeploy with theaz acr build andaz containerapp update commands together to update the container app.

Clean up

All the Azure resources created in this tutorial are in the same resource group. Removing the resource group removes all resources in the resource group and is the fastest way to remove all Azure resources used for your app.

To remove resources, use theaz group delete command.

az group delete --name web-flask-aca-rg

You can also remove the group in theAzure portal or inVisual Studio Code and theAzure Tools Extension.

Next steps

For more information, see the following resources:


Feedback

Was this page helpful?

YesNo

In this article