azure-functionsazure-functions-runtimeazure-functions-docker

Configure the entry point script for an Azure function


I am using a Docker image as my function app container and one thing I want to configure is the path to the entry point of the function app. It is function_app.py in v2 programming model. I am wondering whether there is a way to specify the path or name to such a entry point file.

I hope the entry file path or name can be configurable.


Solution

  • You can run the Python Azure function without mentioning EntryPoint in Dockerfile.

    I have created a Python V2 Azure function.

    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
    

    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
            )
    

    Build the docker image:

    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>          
    

    Run the image in local container:

    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)