为 Azure 函数配置入口点脚本

问题描述 投票:0回答:1

我使用 Docker 映像作为我的函数应用程序容器,我想要配置的一件事是函数应用程序入口点的路径。它是 v2 编程模型中的

function_app.py
。我想知道是否有办法指定这样的入口点文件的路径或名称。

希望入口文件路径或名称可以配置。

azure-functions azure-functions-runtime azure-functions-docker
1个回答
0
投票

无需在 Dockerfile 中提及 EntryPoint,即可运行 Python Azure 函数。

我创建了一个 Python V2 Azure 函数。

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
ENTRYPOINT ./function_app.py
  • docker 容器的入口点由 Azure Functions 运行时管理,该运行时在 Docker 文件的基础映像中指定

    mcr.microsoft.com/azure-functions/python:4-python3.11

  • 函数运行时在

    /home/site/wwwroot
    目录中查找函数文件,通过
    COPY . /home/site/wwwroot
    命令将应用程序代码复制到该目录。

  • 在本地运行

    docker run
    时,Azure Functions 运行时会启动并在
    /home/site/wwwroot
    目录中查找函数定义,并根据定义的触发器(Http、计时器等)管理函数的执行。

function_app.py:

app = func.FunctionApp()

@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

构建docker镜像:

Command: docker build -t <Image_Name> .
C:\Users\uname\pydockerfn>docker build -t <Image> .
[+] Building 1.1s (9/9) FINISHED                                                                                docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                            0.1s
 => => transferring dockerfile: 466B                                                                                            0.1s
 => [internal] load metadata for mcr.microsoft.com/azure-functions/python:4-python3.11                                          0.5s
 => [internal] load .dockerignore                                                                                               0.1s
 => => transferring context: 59B                                                                                                0.0s
 => [1/4] FROM mcr.microsoft.com/azure-functions/python:4-python3.11@XX256:51353837a9b2830XXXd0cc713016da2a6f4bc5  0.0s
 => [internal] load build context                                                                                               0.0s
 => => transferring context: 701B                                                                                               0.0s
 => CACHED [2/4] COPY requirements.txt /                                                                                        0.0s
 => CACHED [3/4] RUN pip install -r /requirements.txt                                                                           0.0s
 => CACHED [4/4] COPY . /home/site/wwwroot                                                                                      0.0s
 => exporting to image                                                                                                          0.1s
 => => exporting layers                                                                                                         0.0s
 => => writing image XX256:7311206332f492a6b1cXXXb684eebd0f4740ec14df5169b                                    0.0s
 => => naming to docker.io/<Image>          

在本地容器中运行镜像:

Command: docker run -p 8080:80 <Image_Name>
C:\Users\uname\pydockerfn>docker run -p 8080:80 -it pravallikakv/pydocerfn

WEBSITES_INCLUDE_CLOUD_CERTS is not set to true.
info: Host.Triggers.Warmup[0]
      Initializing Warmup Extension.
info: Host.Startup[503]
      Initializing Host. OperationId: 'fc439677-86bb-4fa7-b088-ba76773ec9b5'.
info: Host.Startup[504]
      Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=fc439677-86bb-4fa7-b088-ba76773ec9b5
info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]

//Removed few logs

info: Host.Startup[412]
      Host initialized (164ms)
info: Host.Startup[413]
      Host started (199ms)
info: Host.Startup[0]
      Job host started
Hosting environment: Production
Content root path: /azure-functions-host
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
info: Host.General[337]
      Host lock lease acquired by instance ID '0000000000000000000000009ED2BB0B'.
info: Function.http_trigger[1]
      Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=f013216f-aecd-4a4d-b924-4263c153440e)
info: Function.http_trigger.User[0]
      Python HTTP trigger function processed a request.
info: Function.http_trigger[2]
      Executed 'Functions.http_trigger' (Succeeded, Id=f013216f-aecd-4a4d-b924-4263c153440e, Duration=159ms)
© www.soinside.com 2019 - 2024. All rights reserved.