【问题标题】:Diagnose 'Function host is not running.' in docker诊断“功能主机未运行。”在码头
【发布时间】:2021-04-09 10:32:16
【问题描述】:

我正在尝试将我们的几个基于 dotnet 的功能应用程序 (v3) 迁移到 docker 容器。为此,我们使用来自mcr.microsoft.com/azure-functions/dotnet 的图像作为基础

当使用 docker run 进行本地测试时,我经常遇到问题,对容器的 http 调用返回错误 Function host is not running 并结合以下 Docker CLI 输出:

Starting OpenBSD Secure Shell server: sshd.
Hosting environment: Development
Content root path: /home/site/wwwroot
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

这仅仅意味着函数应用不会加载/启动,但真正的原因仍然隐藏。我一直在尝试获取更多日志记录/诊断数据以确定根本原因,但到目前为止失败了。 结果,每次发生这种情况时,我都必须开始一个令人筋疲力尽的试错测试循环。到目前为止,这些情况已经花费了我几天(几周?)

问题:发生此错误时如何获取更多诊断数据?

[更新] Dockerfile:

FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice AS base
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:3.1-buster AS build
WORKDIR /src
# provide access to private nuget (source)
ARG FEED_SOURCE
ARG FEED_ACCESSTOKEN
RUN dotnet new nugetconfig
RUN dotnet nuget add source %FEED_SOURCE% -n PrivateFeed -u docker -p %FEED_ACCESSTOKEN% --store-password-in-clear-text --configfile nuget.config
# Restore all nuget packages by the related .csproj files
COPY ["MyCompany.MyApp.FunctionApp/MyCompany.MyApp.FunctionApp.csproj", "MyCompany.MyApp.FunctionApp/"]
RUN dotnet restore "MyCompany.MyApp.FunctionApp/MyCompany.MyApp.FunctionApp.csproj"

# Copy the rest of the project-folder's content
COPY ["MyCompany.MyApp.FunctionApp", "MyCompany.MyApp.FunctionApp"]
# build the app project
WORKDIR "/src/MyCompany.MyApp.FunctionApp"
RUN dotnet build -c Release --no-restore

FROM build AS publish
RUN dotnet publish -c Release --no-build -o /app/publish

FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AZUREFUNCTIONSJOBHOST__LOGGING__CONSOLE__ISENABLED=true

【问题讨论】:

  • 您找到解决方案了吗? AZUREFUNCTIONSJOBHOST__LOGGING__CONSOLE__ISENABLED=true 应该足够激活 JobHost 的日志,但我同意它没有提供有关问题原因的足够信息(在我的情况下,我可以看到一般异常)
  • 在我的 C# Function App (V3) 中,我注意到大多数隐藏的异常都是在启动期间引发的。这些通常是由无效(环境)配置或连接问题引起的。作为“部分”解决方法,我将所有启动代码放在 try--catch 中,并简单地用Console.Writeline(ex.ToString()); 记录它。在大多数情况下,这给了我足够的继续。对于依赖注入的问题,你可以考虑实现Health Checks

标签: docker .net-core azure-functions azure-webjobs


【解决方案1】:

我遇到了同样的问题,在我发现 TJ Galama 的评论之前,我已经多次阅读了这个帖子。

如何诊断启动失败的答案是将整个启动代码包装在 try/catch 块中:

[assembly: FunctionsStartup(typeof(My.Hr.Functions.Startup))]

namespace My.Hr.Functions;

public class Startup : FunctionsStartup
{
    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
    }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        // try-catch to capture exceptions during startup when running in docker container
        try
        {
            // Register the core services.
            builder.Services
                .AddSingleton<HrSettings>()
                .AddExecutionContext();

            // More setup code ...
        }
        catch (System.Exception ex)
        {
            System.Console.Error.WriteLine(ex);
            throw;
        }

    }
}

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多