This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
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.
To complete this tutorial, you need:
An Azure account where you can deploy a web app toAzure Container Apps. (AnAzure Container Registry andLog Analytics workspace are created for you in the process.)
Azure CLI,Docker, and theDocker CLI installed in your local environment.
In your local environment, get the code.
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
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 the image locally.
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.
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:
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.
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.
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.
You can also remove the group in theAzure portal or inVisual Studio Code and theAzure Tools Extension.
For more information, see the following resources:
Was this page helpful?