Search code examples
dockerflaskazure-functionsazure-durable-functionsquart

Facing issue in deploying a Quart application in Azure Durable Funtions with Docker


I am trying to deploy a python Quart application to Azure function with Docker. Below is my Docker file. My Azure function is failing to start. I get "Application error" when accessing the function URL. I would appreciate any help here in verifying if my dockerfile and main function is correct

Dockerfile:

FROM mcr.microsoft.com/azure-functions/python:4-python3.11
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \    AzureFunctionsJobHost__Logging__Console__IsEnabled=true# Copy the function app files to the appropriate location in the container
COPY . /home/site/wwwroot# Install dependencies
RUN pip install --upgrade pip
RUN pip install -r /home/site/wwwroot/requirements.txt
EXPOSE 8080

WrapperFuntion--> init.py:

import azure.functions as func
import azure.durable_functions as duf

from common_util.utilities import logger
from FlaskApp import flask-app
from quart import g


async def main(req: func.HttpRequest, mappingColumnsConfig: func.InputStream, starter: str, context: func.Context) -> func.HttpResponse:
    async with flask_app.app_context():
        
        g.client = duf.DurableOrchestrationClient(starter)
        g.mapping_columns_config = mappingColumnsConfig
        g.function_name = req.route_params["functionName"]
        
        
        response = await func.AsgiMiddleware(app=flask-app.asgi_app).handle_async(req, context)
        
        status_code = response.status_code
        headers = dict(response.headers)
        body = response.get_body()
        
        
        return func.HttpResponse(body, status_code=status_code, headers=headers)

FlaskApp--> init.py:

from quart import Quart
from quart_cors import cors


flask-app = Quart(__name__)
flask-app = cors(flask-app)


@app.post("/api/pets/upload-petdata")
async def upload_petdata_route():
    try:
        ----DB insert----

    except Exception as e:
    logger.error(f"Pets data inert operation failed")

I tried running the function with func start and its running. But if I try to run the function with docker in local I am getting "Function host not running" . If I deploy the container directly to Azure function , the function is failing to start


Solution

  • I get "Application error" when accessing the function URL.

    This error occurs if the function is not deployed properly. Check the kudu site of the function app if you can see all the files of your function.

    I have created a sample Python Durable Azure functions with Quart.

    Code Snippet:

    import azure.durable_functions as duf
    import azure.functions as func
    from quart import g
    
    from flaskApp import flask_app
    
    async def main(req: func.HttpRequest,  context: func.Context) -> func.HttpResponse:
    
        response = await func.AsgiMiddleware(app=flask_app.asgi_app).handle_async(req, context)
        
        status_code = response.status_code
        headers = dict(response.headers)
        body = response.get_body()
    
        return func.HttpResponse(body, status_code=status_code, headers=headers)
    

    flaskApp=>init.py:

    import azure.functions as func
    from quart import Quart
    from quart_cors import cors
    
    flask_app = Quart(__name__)
    flask_app = cors(flask_app)
    
    @flask_app.post("/api/pets/upload-petdata")
    async def upload_petdata_route():
        try:
         return {"message": "Pet data inserted successfully!"}, 200
        except Exception as e:
            return {"error": "Pets data insert operation failed"}, 500
    

    Dockerfile:

    FROM mcr.microsoft.com/azure-functions/python:4-python3.11
    
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
        AzureFunctionsJobHost__Logging__Console__IsEnabled=true
    
    COPY requirements.txt /
    RUN pip install -r /requirements.txt
    
    COPY . /home/site/wwwroot
    

    local Response:

    enter image description here

    Build the docker image:

    Able to run the docker image of the function with the command docker run -p 8080:80 <docker_image>:

    enter image description here

    enter image description here

    Add the required Application settings in the function app.

    Deployed to Azure:

    enter image description here

    Portal:

    enter image description here

    enter image description here