Share via

How to deploy flask application on azure app service

M R, Gouthami (623) 0 Reputation points
2026-03-11T08:22:52.6966667+00:00

How to deploy flask application on azure app service

Azure App Service
Azure App Service

Azure App Service is a service used to create and deploy scalable, mission-critical web apps.


3 answers

Sort by: Most helpful
  1. Shree Hima Bindu Maganti 7,190 Reputation points Microsoft External Staff Moderator
    2026-03-21T08:24:06.63+00:00

    Hi @M R, Gouthami (623)
    The recommended way to deploy a Flask application on Azure App Service is to use a Linux App Service with built-in Python runtime, since Python on Windows is no longer supported for new deployments. Ensure your project contains a main Flask file (for example, app.py) with the Flask instance exposed (app = Flask(__name__)) and a requirements.txt file listing all dependencies (pip freeze > requirements.txt). Test the application locally using a virtual environment and run it with flask run before deployment. You can then deploy directly from your project folder using Azure CLI with a command such as az webapp up --name <unique-app-name> --resource-group <resource-group> --runtime "PYTHON|3.9" --sku B1, which automatically creates the required App Service resources and deploys your code. After deployment, configure a startup command in Configuration >General Settings like gunicorn --bind=0.0.0.0:8000 app:app (format: <filename>:<FlaskAppObject>) and restart the app. You may also add environment variables under Application Settings and use Log Stream to troubleshoot startup issues.
    https://learn.microsoft.com/en-us/azure/app-service/quickstart-python?tabs=flask%2Cwindows%2Cazure-cli%2Cazure-cli-deploy%2Cdeploy-instructions-azportal%2Cterminal-bash%2Cdeploy-instructions-zip-azcli
    https://learn.microsoft.com/en-us/azure/app-service/configure-language-python
    https://learn.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app-flask?tabs=copilot&pivots=azure-portal
    Let me know if you have any further assistance needed.

    0 comments No comments

  2. Golla Venkata Pavani 3,900 Reputation points Microsoft External Staff Moderator
    2026-03-11T10:59:59.1133333+00:00

    Hii @M R, Gouthami (623),

    Thanks for reaching out about deploying your Flask app to Azure App Service. The most straightforward and recommended way (per the latest Microsoft guidance) is to use a Linux-based App Service with the built-in Python support, Python on Windows is no longer supported for new setups.

    Azure App Service automatically detects Flask apps and starts them using Gunicorn (the platform includes it by default), so you usually don't need a custom startup command if your setup follows the standard pattern.

    Quick Steps:

    1. Prepare the Flask Application

    Ensure your Flask app follows these requirements:

    • The main file is typically named app.py (or similar).
    • The Flask app object must be exposed (for example: app = Flask(__name__)).
    • A requirements.txt file must exist at the project root listing all dependencies.
        from flask import Flask
         
        app = Flask(__name__)
        @app.route("/")
        def home():
            return "Hello from Flask on Azure!"
        if __name__ == "__main__":
            app.run()
      
    • Create requirements.txt:
        pip freeze > requirements.txt
      
      Azure App Service automatically creates a virtual environment and installs dependencies from requirements.txt during deployment.
    1. Test the App Locally
    Before deploying, verify the app works locally:
    python -m venv .venv
    source .venv/bin/activate   # Linux/macOS
    pip install -r requirements.txt
    flask run
    

    Browse to http://localhost:5000 to confirm the app runs successfully.\

    1. Create Azure App Service Resources

    You can create and deploy the app using Azure CLI, VS Code, or the Azure Portal.

    Using Azure CLI (Recommended):

    • Sign in: az login
    • Deploy directly from your project folder:
        az webapp up --name <unique-app-name> --resource-group <resource-group> --runtime "PYTHON|3.9" --sku B1
      
    • This command automatically: Creates a resource group (if not existing) ,Creates a Linux App Service plan, Creates the Web App, Deploys your Flask code
    1. Configure the Startup Command (Important)

    Flask apps must be started using a production server such as Gunicorn.

    In Azure Portal:

    • Go to App Service > Configuration > General Settings
    • Set Startup Command: gunicorn --bind=0.0.0.0:8000 app:app (app:app<filename>:<FlaskAppObject>)
    • Save and restart the app.
    1. Configure Environment Variables (Optional)

    For secrets or configuration values:

    • Azure Portal > Configuration > Application settings
    • Add environment variables (for example, database connection strings)

    These values are available in Flask via os.environ.

    1. Verify Deployment
    • Open the application: https://<app-name>.azurewebsites.net
    • Use Log Stream under Monitoring to troubleshoot startup or runtime errors.

    Reference:
    https://learn.microsoft.com/en-us/azure/app-service/quickstart-python
    https://learn.microsoft.com/en-us/azure/app-service/configure-language-python
    https://learn.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app-flask?tabs=copilot&pivots=azure-portal

    Please let us know if you find the above helpful or if you need further assistance with this issue.

    If the information was helpful, Please "accept." This will help us and other members of the community as well.

    0 comments No comments

  3. Q&A Assist
    2026-03-11T08:23:12.96+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To deploy a Flask application on Azure App Service, use one of these supported approaches.

    1. Deploy a standard Flask app to App Service (code-based)

    1. Get or create a Flask app.
      • Example sample app:
             git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart
             cd msdocs-python-flask-webapp-quickstart
        
    2. Run locally (optional) to verify it works:
      • Create and activate a virtual environment, then install dependencies and run:
             python3 -m venv .venv
             source .venv/bin/activate   # macOS/Linux
             # or on Windows
             # py -m venv .venv
             # .venv\scripts\activate
             
             pip install -r requirements.txt
             flask run
        
      • Browse to http://localhost:5000.
    3. Create an App Service and deploy.
      • In Azure, create:
        • A Resource Group
        • An App Service Plan (Linux, for Python)
        • A Web App (runtime stack: Python)
      • Use your preferred deployment method (VS Code extension, Azure CLI, or GitHub Actions) to push the Flask project folder to the Web App.
      • App Service will install packages from requirements.txt and start the app with the configured startup command.

    2. Deploy a Flask app with PostgreSQL (App Service + Azure Database for PostgreSQL)

    1. Use the Flask + PostgreSQL sample.
      • In the portal tutorial, the sample repo msdocs-flask-postgresql-sample-app is used.
    2. Configure CI/CD with GitHub Actions:
      • In the Web App → Deployment Center:
        1. Set Source to GitHub (build provider: GitHub Actions).
        2. Select your GitHub organization, the msdocs-flask-postgresql-sample-app repository, and the starter-no-infra branch.
        3. Choose User-assigned identity for authentication and Save.
      • This creates a workflow file in .github/workflows that builds and deploys to App Service on each git push.
    3. Configure database connection in the app (when using the azd-based flow):
      • In azureproject/production.py, use the App Service connection settings:
             DATABASE_URI = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
                 dbuser=os.getenv('AZURE_POSTGRESQL_USER'),
                 dbpass=os.getenv('AZURE_POSTGRESQL_PASSWORD'),
                 dbhost=os.getenv('AZURE_POSTGRESQL_HOST'),
                 dbname=os.getenv('AZURE_POSTGRESQL_NAME')
             )
        
      • Redeploy with:
             azd deploy
        

    3. Deploy a containerized Flask app to App Service (Linux containers)

    1. Start from the Flask sample and add container files:
         git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart.git
         cd msdocs-python-flask-webapp-quickstart
      
    2. Add a Dockerfile using Gunicorn:
         # syntax=docker/dockerfile:1
         FROM python:3.11
         WORKDIR /code
         COPY requirements.txt .
         RUN pip3 install -r requirements.txt
         COPY . .
         EXPOSE 50505
         ENTRYPOINT ["gunicorn", "app:app"]
      
      • Ensure requirements.txt includes:
             Flask==3.1.0
             gunicorn
        
    3. (Optional) Add .dockerignore:
         .git*
         **/*.pyc
         .venv/
      
    4. Build and test locally:
         docker build --tag flask-demo .
         docker run --detach --publish 5000:50505 flask-demo
         # browse to http://localhost:5000
      
    5. Push image to Azure Container Registry (ACR):
      • Build in ACR:
             az acr build \
               --resource-group $RESOURCE_GROUP_NAME \
               --registry $CONTAINER_REGISTRY_NAME \
               --image webappsimple:latest .
        
    6. Create an App Service plan and Web App for containers:
         az appservice plan create \
           --name webplan \
           --resource-group $RESOURCE_GROUP_NAME \
           --sku B1 \
           --is-linux
      
      • Then create the Web App with az webapp create, pointing to the ACR image and enabling managed identity and AcrPull role as described in the tutorial. App Service will pull and run the container.

    Once deployed (by any of these methods), access the Flask app via the Web App’s default URL: https://<your-app-name>.azurewebsites.net.


    References:

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.