Help deploying a Dockerised Python Quart Application to Azure functions App

Nayak, Rajesh 65 Reputation points
2024-10-17T14:40:31.4833333+00:00

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")

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,072 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 4,535 Reputation points Microsoft Employee
    2024-10-18T09:01:17.8533333+00:00

    Hello @Nayak, Rajesh

    Based on the provided information, it seems that the issue is with the main function in the WrapperFuntion--> init.py file. The function is not returning a valid HTTP response object.

    Here are a few things you can try to resolve the issue:

    1. Make sure that the functionName variable is defined before it is used in the main function. You can define it as a string literal or import it from a module.
    2. Wrap the response body in a bytes object before returning it in the HttpResponse object. For example, you can modify the return statement as follows:
    return func.HttpResponse(body.encode(), status_code=status_code, headers=headers)
    
    1. Make sure that the Flask app is running on the correct port. By default, Azure Functions expects the app to be running on port 80. You can modify the Flask app to listen on port 80 by adding the following line of code: flask-app.run(host='0.0.0.0', port=80)
    2. Alternatively, you can modify the Dockerfile to expose port 8080 instead of port 80 by changing the EXPOSE directive as follows: EXPOSE 8080 Then, you can modify the main function to return an HTTP response with status code 200 and a message indicating that the app is running.

    For example:

    return func.HttpResponse("Flask app is running on port 8080", status_code=200)
    

    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.


Your answer

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