Deploy a Python Streamlit app with Azure App Service
For ease of deployment, we will use Azure App Service for Linux which allows developers to deploy web applications with a PaaS experience, similar to Heroku.
Supported web frameworks in Python are Flask and Django. For other non-supported frameworks such as FastAPI, a Docker image is required.
First step is to make sure a ressource group is available. If not, a new one can be created:
az group create \
--name tutorial-azure-app-service-group \
--location westeurope
As Flask is one of the two Python frameworks natively supported by Azure App Service (the other one being Django), we are going to take advantage of this and deploy a simple Flask app using Git!
Documentation:
- Quickstart Azure App Service for Python
- Configure Python for Azure App Service
- Azure App Service deploy with Git
- Azure App Service configure credentials
- Configure a deployment user (might require
git config --system --unset credential.helper
before-hand):
az webapp deployment user set --user-name <username> --password <password>
- Create a Git enabled app
az appservice plan create \
--name tutorial-azure-app-service-flask-plan \
--resource-group tutorial-azure-app-service-group \
--is-linux \
--sku F1
az webapp create \
--name tutorial-azure-app-service-flask-app \
--resource-group tutorial-azure-app-service-group \
--plan tutorial-azure-app-service-flask-plan \
--runtime "PYTHON|3.8" \
--deployment-local-git
- Configure the deployment branch:
az webapp config appsettings set \
--name tutorial-azure-app-service-flask-app \
--resource-group tutorial-azure-app-service-group \
--settings DEPLOYMENT_BRANCH='master'
- Add the Git remote:
git remote add azure-flask https://tutorial-azure-app-service-flask-app.scm.azurewebsites.net/tutorial-azure-app-service-flask-app.git
- Deploy to Azure:
git push azure-flask master
-
Open the app: https://tutorial-azure-app-service-flask-app.azurewebsites.net
-
To delete the app:
az webapp delete \
--name tutorial-azure-app-service-flask-app \
--resource-group tutorial-azure-app-service-group
In order to deploy a Streamlit app on Azure App Service, we will need to build and deploy a Docker container.
Documentation: Azure App Service Custom Container
- Build and run the Docker image locally:
docker build -t tutorial-azure-app-service-streamlit-app:latest .
docker run --rm -it -p "5000:5000" tutorial-azure-app-service-streamlit-app:latest
- Create an Azure Container Registry to push the Docker image:
az acr create \
--name tutorialazureappserviceregistry \
--resource-group tutorial-azure-app-service-group \
--sku Basic \
--admin-enabled true
- Get the credentials for Azure Container Registry:
az acr credential show \
--name tutorialazureappserviceregistry \
--resource-group tutorial-azure-app-service-group
- Log into Azure Container Registry using Docker:
docker login tutorialazureappserviceregistry.azurecr.io --username tutorialazureappserviceregistry
- Tag and push the Docker image to Azure Container Registry:
docker tag tutorial-azure-app-service-streamlit-app tutorialazureappserviceregistry.azurecr.io/tutorial-azure-app-service-streamlit-app:latest
docker push tutorialazureappserviceregistry.azurecr.io/tutorial-azure-app-service-streamlit-app:latest
- Verify the Docker image has been pushed:
az acr repository list --name tutorialazureappserviceregistry
- Create the Azure App Service (WARNING: the F1 SKU does not seem to be working with Streamlit and Websockets in general, use at least B1, cf. GitHub issue):
az appservice plan create \
--name tutorial-azure-app-service-streamlit-plan \
--resource-group tutorial-azure-app-service-group \
--is-linux \
--sku B1
az webapp create \
--name tutorial-azure-app-service-streamlit-app \
--resource-group tutorial-azure-app-service-group \
--plan tutorial-azure-app-service-streamlit-plan \
--deployment-container-image-name tutorialazureappserviceregistry.azurecr.io/tutorial-azure-app-service-streamlit-app:latest
az webapp config appsettings set \
--name tutorial-azure-app-service-streamlit-app \
--resource-group tutorial-azure-app-service-group \
--settings WEBSITES_PORT=5000
-
Open the app: https://tutorial-azure-app-service-streamlit-app.azurewebsites.net
-
Turn on container logging:
az webapp log config \
--name tutorial-azure-app-service-streamlit-app \
--resource-group tutorial-azure-app-service-group \
--docker-container-logging filesystem
- Display container logging:
az webapp log tail \
--name tutorial-azure-app-service-streamlit-app \
--resource-group tutorial-azure-app-service-group
- Whenever a new Docker image is pushed to the registry, restart the App Service:
az webapp restart \
--name tutorial-azure-app-service-streamlit-app \
--resource-group tutorial-azure-app-service-group