Trying to deploy a Dash app on Azure Web app

I have been trying for days to deplpy a dash app on an Azure web app. I have tried following guides (for example this) but I failed, both deploying via Github or via Azure CLI.

My app is quite simple, it uses a Dash object named app and in the main I have:

if __name__ == '__main__':
    server = app.server 
    app.run_server()

Then, I do az webapp up (or Github whatever) and I upload it on a Webapp. Nothing. I even tried to set up the startup command from the web app to call gunicorn but it didn’t work. My application doesn’t start, I get application error (same here).
The deployment is ok (no errors on the logs).

I hoped that this part would be the simplest (I have set up before my Azure functions and Synapse to retrieve and process the data) but I was evidently wrong.

Do you have ideas or complete working examples to share?

Hello @matteodefelice,

Thank you for creating another topic.

Try this instead:

server = app.server 

if __name__ == '__main__':
    app.run_server()

Thank you for your reply. I have tried with this but I still get the Application Error once setting gunicorn app:server

Are all your requirements downloading properly, can you run your app locally if using gunicorn, or running the server via waitress?

Honestly, I always try to test my Dash app just running the app from Python (python app.py). I don’t know other ways…should I try then using gunicorn locally on my windows machine?

Yes, to see if your configuration runs under normal circumstances.

If you can’t use waitress with the server, then something isn’t right with your app set up. If it runs, it may be an issue with your Azure app installing the requirements.txt.

Hello sir! I’ve tried to deploy the same code as @matteodefelice on the Azure Webapp service and I’m facing an odd issue in the deployment; specifically, my app deployment is failing at the following hurdles:

  1. ModuleNotFoundError: No module named ‘app’

I think this can be traced back to:
2. WARNING: Could not find virtual environment directory /home/site/wwwroot/antenv.

I have the application setting SCM_DO_BUILD_DURING_DEPLOYMENT set to True. Do you have any ideas why I might be facing that?

Thank you so much.

Hello @milandeleev,

Welcome to the community!

Without seeing how you are setting up your app, its hard to know where something is off.

On azure, you need to make sure that you have gunicorn pulling the server, and not the app from the dash app.

Hey @jinnyzor ! Thanks for the kind welcome. I’m setting up my app exactly as in this thread:

which preceded this thread. I’ve cloned this git repo:

Added the server = app.server line, set it up on Azure Webapps with Python 3.10, and then changed the startup command to gunicorn --bind=0.0.0.0 --timeout 600 app:server. Unfortunately, I am getting the errors described above.

Hmm. Is your repo being loaded properly?

To say that the app is missing, either the repo is failing or gunicorn isn’t working right.

I feel your pain matteodefelice. I’m having exactly the same issue:

I am using the latest version of VSCode and have written the simplest of dash apps:

'''   
from dash import Dash, dcc, html
app = Dash(__name__)
app.layout = html.Div([
dcc.Markdown(''' ### Ayup python world ''')
])
if __name__ == '__main__'
    app.run_server()
'''

This runs on my localhost without issue.

I have a folder on my desktop named “practice” and I used “python -m venv venv” to set up a virtual environment. I then activated the venv using “venv\Scripts\activate”, downloaded dash using “pip install dash” and generated a requirements.txt file “pip freeze > requirements.txt”.

folder structure:

'''
practice:
>venv
app.py
requirements.txt
'''

To learn how to use Microsoft Azure I set-up a free trail acount and also installed Docker on my laptop.

I then installed the Azure and Docker extensions in VScode and then generated an image of my app using Docker:

CTRL-SHIFT P Docker Images: Build Image…

Couldn’t find a Dockerfile in your workspace. Would you like to add Docker files to the workspace? Python General Enter a Python module instead - app.py include optional Docker Compose files? - No

Here’s the Dockerfile:

'''
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10-slim

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "-m", "app.py"]
'''

New folder structure ‘’’ practice: >venv >.vscode .dockerignore app.py Dockerfile requirements.text ‘’’ After running Docker Images the app ran on my localhost with no issues.

I then went to my Azure subscription and setup a container registry, a resource group and an App service:

Create a resource Container Registry - Create Subscription - Azure subscription 1 Resource group - Create new - mick-practice-resource-group Registry name - mickPracticeRegistry Location - Canada Central Pricing plan - Basic Review and Create - Validation passed Create - Deployment succeeded

App Services Create - Web App Subscription - Azure subscription 1 Resource group - Create new - mick-practice-resource-group Name - mickPracticeApp Publish - Docker Container Operating System - Linux Location - Canada Central Linux plan - (New) ASP-mickpracticeresorucegroup-809e Pricing plan - Free F1 (Shared infrastructure) Zone redundancy - Disabled Review and Create Create - Your deployment is complete

I then went back to VSCode to the Docker extension: under Images Right Click on Practice:latest Push - mickpracticeregistry.azurecr.io/practice:latest Push successful

App opened on http://127.0.0.1:8050/ without issue

In Docker went to Registries, clicked connect icon and connected. clicked refresh and practice folder appeared dropdown revealed latest right clicked on latest and selected Deploy Image to Azure App Service… Name: mickPracticeWebApp Resource group: mick-practice-resource-group Service plan: ASP-mickpracticeresorucegroup-809e

Successfully created web app “mickPracticeWebApp” https://mickpracticewebapp.azurewebsites.net

Clicked Open Site

Do you want Code to open external website? Open

New window opens with Loading and circling ellipse until “:frowning: Application Error If you are the application administrator, you can access the diagnostic resources.”

I have spent a couple of days trying to understand what’s happening to no avail. I think this has something to do with the app.run_server() line. I have also tried with app.run_server(host=‘0.0.0.0’, port='8050) and app.run_server(host=‘0.0.0.0’, port='80).

It’s driving me crazy. I’ve scoured the web for an answer but nothing works. Have you had any feedback that works?

This worked for me!!!

Azure App Service runs your application as a web server, and it typically expects the web server to bind to the port specified by the PORT environment variable, which is dynamically assigned by Azure. Instead of hardcoding the port number in your app.run_server() call, you should modify your code to use the PORT environment variable provided by Azure. Here’s how you can do it:

  1. Modify your app.run_server() call in your app.py file like this:

pythonCopy code

if __name__ == '__main__':
    app.run_server(host='0.0.0.0', port=int(os.environ.get('PORT', 8050)))

This code sets the port to the value of the PORT environment variable if it exists, or defaults to port 8050 if not.

  1. You need to import the os module at the top of your app.py file to use os.environ.

pythonCopy code

import os

Make sure these changes are made in your local app.py file.

  1. Also, ensure that your Dockerfile is configured to use the correct CMD for running the app:

DockerfileCopy code

CMD ["python", "app.py"]
  1. After making these changes, rebuild your Docker image and push it to your container registry again.
  2. Deploy the updated Docker image to Azure App Service.

These changes should allow your Dash app to bind to the correct port when deployed to Azure App Service and resolve the “Application Error” issue.

Additionally, make sure your Azure App Service is configured correctly to run a Docker container and that your Docker image is successfully deployed to Azure. Check the Azure portal for any error messages or logs that might provide more information about the issue if it persists.