7

I am trying to migrate several of our dotnet based function-apps (v3) to docker containers. For this we use images from mcr.microsoft.com/azure-functions/dotnet as a base

When testing locally using docker run, I often have the problem a http call to the container returns error Function host is not running combined with the following Docker CLI output:

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.

This simply means the function app won't load / start but the real cause stays hidden. I have been trying to get more logging/ diagnostics data to determine the underlying cause but failed so far. As a result, I have to start an exhausting trial-and-error test loop, everytime this situation occurs. These situations have taken me several days (weeks?) so far

Question: How do I get more diagnostic data when this error occurs?

[UPDATE] the 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
TJ Galama
  • 457
  • 3
  • 12
  • Did you find any solution for this? AZUREFUNCTIONSJOBHOST__LOGGING__CONSOLE__ISENABLED=true should be enough activate logs for JobHost, but I agree that it doesn't provide enough information about the cause of the problems (in my case I can see a generic exception) – HAL9000 Jan 19 '22 at 14:19
  • 1
    Within my C# Function App (V3), I noticed that a most of the hidden exceptions are raised during startup. These are often caused by invalid (environment) configurations or connection issues. As a 'partial' workaround I placed all startup code within a try--catch and simply logged it with `Console.Writeline(ex.ToString());`. In most cases this gave me enough to continue. For problems with dependency injection you might consider implementing [Health Checks](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-6.0) – TJ Galama Feb 01 '22 at 19:52

1 Answers1

0

I had the same issue and went over this thread many times before I figured out the comment from TJ Galama.

The answer to how to diagnose startup failures is to wrap the entire startup code in try/catch block:

[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;
        }

    }
}
Karpik
  • 322
  • 1
  • 5
  • 14