Creating Azure Functions using .NET 5 and Docker

Viewed 998

There is some Azure Function that uses an Isolated Model, running as an out-of-process language worker that is separate from the Azure Functions runtime. Because Azure Functions runtime doesn't support .NET5 yet.

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>

I'm looking for a way how to deploy .NET func as a Docker container. enter image description here

func init LocalFunctionsProject --worker-runtime dotnet-isolated --docker 

For .NET 3.1 I had Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

FROM mcr.microsoft.com/azure-functions/dotnet:3.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

How to containerize .NET5 func? Is it even possible? Any workarounds? I've not found a solution yet. Please suggest

1 Answers

OK, so there are two things going on:

  1. There is still a dependency on the .NET 3.1 Core SDK even in .NET 5.0 Functions projects, which seems to be "by design."
  2. The Dockerfile that we generate for a project with --worker-runtime = dotnet-isolated --docker is missing a key line to add the reference to .NET Core 3.1.

To fix this, add the following lines after the first FROM statement in the Dockerfile:

# Build requires 3.1 SDK
COPY --from=mcr.microsoft.com/dotnet/core/sdk:3.1 /usr/share/dotnet /usr/share/dotnet

Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS installer-env
COPY --from=mcr.microsoft.com/dotnet/core/sdk:3.1 /usr/share/dotnet /usr/share/dotnet

COPY ./LocalFunctionsProject/ /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

FROM mcr.microsoft.com/azure-functions/dotnet-isolated:3.0-dotnet-isolated5.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

The docs will be updated soon

Related