dockerasp.net-coreazure-functionsazure-functions-isolated

Running Isolated-worker Azure Function in Docker Container


I'm trying to get an isolated worker model Azure function to run in a container, but it fails with the following error:

Unhandled exception. System.InvalidOperationException: The gRPC channel URI 'http://:' could not be parsed.

Here is my dockerfile

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

WORKDIR /src

COPY ["Email.Function/Email.Function.csproj", "Email.Function/"]
RUN dotnet restore "Email.Function/Email.Function.csproj"

COPY . .

RUN dotnet build "Email.Function/Email.Function.csproj" -c Release -o /app/build

RUN dotnet publish "Email.Function/Email.Function.csproj" -c Release -o /app/publish

FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0

WORKDIR /home/site/wwwroot

COPY --from=build /app/publish .

CMD ["dotnet", "Email.Function.dll", "--dotnet-isolated"]

I saw some workarounds was to add the following environment variables but this also didn't work

ENV FUNCTIONS_WORKER_GRPC_URI=http://127.0.0.1:5000
ENV WEBSITE_USE_PLACEHOLDER_DOTNETISOLATED=0

I can run and debug the function without using docker, but it fails inside a container.


Solution

  • Unhandled exception. System.InvalidOperationException: The gRPC channel URI 'http://:' could not be parsed.

    The error could be due to the misconfiguration with the communication between the Azure Functions worker and the runtime.

    I have created a .NET 8.0 Dotnet-isolated Azure function and dockerized the function using the Dockerfile as below:

    Dockerfile:

    # This stage is used when running from VS in fast mode (Default for Debug configuration)
    
    FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base
    WORKDIR /home/site/wwwroot
    EXPOSE 8080
    
    # This stage is used to build the service project
    FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["FunctionApp/FunctionApp.csproj", "FunctionApp/"]
    RUN dotnet restore "./FunctionApp/FunctionApp.csproj"
    COPY . .
    WORKDIR "/src/FunctionApp"
    RUN dotnet build "./FunctionApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
    
    # This stage is used to publish the service project to be copied to the final stage
    FROM build AS publish
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet publish "./FunctionApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
    
    # This stage is used in production or when running from VS in regular mode
    FROM base AS final
    WORKDIR /home/site/wwwroot
    COPY --from=publish /app/publish .
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
        AzureFunctionsJobHost__Logging__Console__IsEnabled=true
    

    Docker image for the function:

    4-dotnet-isolated8.0: Pulling from azure-functions/dotnet-isolated
    302e3ee49805: Pull complete
    fd3debd68516: Pull complete
    e31c284582ca: Pull complete
    ed4b264aaa40: Pull complete
    6abaf3c9f6d3: Pull complete
    11f569edae72: Pull complete
    dcd258fee610: Pull complete
    85cdbb6993b0: Pull complete
    e821cb5f22ed: Pull complete
    fe23fcab6c1a: Pull complete
    Digest: sha256:eeb2724e49f0e7c8c978bd60ee15a2b87714b2376bcc0731af9e09c34385ae1a
    Status: Downloaded newer image for mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0
    mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0
    

    Able to run the function in container:

    Azure Functions Core Tools
    Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
    Function Runtime Version: 4.834.3.22875
    
    [2024-10-14T13:29:31.498Z] Found /home/site/wwwroot/FunctionApp57.csproj. Using for user secrets file configuration.
    Skipping 'AzureWebJobsStorage' from local settings as it's already defined in current environment variables.
    Skipping 'FUNCTIONS_WORKER_RUNTIME' from local settings as it's already defined in current environment variables.
    Skipping 'AzureWebJobsScriptRoot' from local settings as it's already defined in current environment variables.
    [2024-10-14T13:29:38.042Z] Extension Bundle not loaded. Loading extensions from /home/site/wwwroot/bin/Debug/net8.0. BundleConfigured: False, PrecompiledFunctionApp: False, LegacyBundle: False, DotnetIsolatedApp: True, isLogicApp: False
    [2024-10-14T13:29:38.046Z] Script Startup resetting load context with base path: '/home/site/wwwroot/bin/Debug/net8.0/.azurefunctions'.
    [2024-10-14T13:29:38.072Z] Loading startup extension 'Startup'
    [2024-10-14T13:29:38.674Z] Loaded extension 'Startup' (1.0.0.0)
    [2024-10-14T13:29:40.912Z] Worker indexing is enabled
    [2024-10-14T13:29:40.926Z] Fetching metadata for workerRuntime: dotnet-isolated
    [2024-10-14T13:29:40.927Z] Reading functions metadata (Worker)
    { "name":"dotnet-worker-startup", "workerProcessId" : 109 }
    [2024-10-14T13:29:46.936Z] 1 functions found (Worker)
    [2024-10-14T13:29:47.041Z] Azure Functions .NET Worker (PID: 109) initialized in debug mode. Waiting for debugger to attach...
    [2024-10-14T13:29:47.200Z] Worker process started and initialized.
    
    Functions:
    
        Function1: [GET,POST] http://localhost:32047/api/Function1
    
    For detailed output, run func with --verbose flag.
    [2024-10-14T13:29:52.541Z] Host lock lease acquired by instance ID '0000000000000000000000004720E01E'.
    [2024-10-14T13:30:14.263Z] Executing HTTP request: {
    
    [2024-10-14T13:30:14.263Z]   "requestId": "d2d043f7-de5e-4b5c-be15-ceff675a5e1c",
    
    [2024-10-14T13:30:14.263Z]   "method": "GET",
    
    [2024-10-14T13:30:14.263Z]   "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
    
    [2024-10-14T13:30:14.263Z]   "uri": "/api/Function1"
    
    [2024-10-14T13:30:14.263Z] }
    [2024-10-14T13:30:14.806Z] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=7e0e50d7-cdfb-44d0-966c-bd7cc405a456)
    [2024-10-14T13:30:15.688Z] C# HTTP trigger function processed a request.
    [2024-10-14T13:30:15.709Z] Executing OkObjectResult, writing value of type 'System.String'.
    [2024-10-14T13:30:16.001Z] Executed 'Functions.Function1' (Succeeded, Id=7e0e50d7-cdfb-44d0-966c-bd7cc405a456, Duration=1281ms)
    [2024-10-14T13:30:16.108Z] Executed HTTP request: {
    
    [2024-10-14T13:30:16.108Z]   "requestId": "d2d043f7-de5e-4b5c-be15-ceff675a5e1c",
    
    [2024-10-14T13:30:16.108Z]   "identities": "",
    
    [2024-10-14T13:30:16.108Z]   "status": "200",
    
    [2024-10-14T13:30:16.108Z]   "duration": "1841"
    
    [2024-10-14T13:30:16.108Z] }