我正在尝试使用 Docker 将 python Quart 应用程序部署到 Azure 函数。下面是我的 Docker 文件。我的 Azure 功能无法启动。访问函数 URL 时出现“应用程序错误”。如果我能帮助验证我的 dockerfile 和 main 函数是否正确,我将不胜感激
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")
我尝试使用 func start 运行该函数并运行它。但是,如果我尝试在本地使用 docker 运行该函数,我会收到“函数主机未运行”。如果我将容器直接部署到 Azure 函数,该函数将无法启动
访问函数 URL 时出现“应用程序错误”。
如果该功能未正确部署,就会出现此错误。检查函数应用程序的 kudu 站点是否可以看到函数的所有文件。
我使用 Quart 创建了一个示例 Python Durable Azure 函数。
代码片段:
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
当地回应:
在.
中添加所需的环境变量部署到Azure:
传送门: