使用 .NET 5 和 Docker 创建 Azure Functions

Creating Azure Functions using .NET 5 and Docker

有一些 Azure Functions 使用独立模型,运行 as an out-of-process language worker 与 Azure Functions 运行时分开。因为 Azure Functions 运行时还不支持 .NET5。

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

我正在寻找一种将 .NET func 部署为 Docker 容器的方法。

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

对于 .NET 3.1,我有 Docker文件

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"]

如何容器化 .NET5 函数?有可能吗?任何解决方法?我还没有找到解决方案。请推荐

好的,所以有两件事正在发生:

  1. 即使在 .NET 5.0 Functions 项目中仍然存在对 .NET 3.1 Core SDK 的依赖,seems to be "by design."
  2. 我们为项目生成的 Dockerfile --worker-runtime = dotnet-isolated --docker is missing a key line to add the reference to .NET Core 3.1.

要解决此问题,请在 Dockerfile 中的第一个 FROM 语句后添加以下行:

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

Docker 文件:

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即将更新