DEV Community

Daniel Reis
Daniel Reis

Posted on • Originally published at

Running Azure Functions with .NET 5 on Docker

The Azure Functions .NET worker has been out for a while, and it finally enabled developers to use C# 9 and .NET 5 on function apps. This is a new way to create C# function apps, it now has a Program.cs class, with the good old generic Host builder and all the extension methods that we're used to see on regular ASP.NET Core and Worker service projects. This is how it can look:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureAppConfiguration(builder => builder.AddJsonFile("appsettings.json"))

await host.RunAsync();
Enter fullscreen mode Exit fullscreen mode

Note that this is using top-level statements. This came with C# 9 and it basically hides the Program class and its namespace, removing some boilerplate and making it easier to understand for newcomers, looks nice!

If you want to run the .NET 5 function apps on Docker containers, as of the day of writing this, the Dockerfile generated by func init --docker --worker-runtime dotnetIsolated does not build correctly, you'll encounter the following error:

The framework 'Microsoft.NETCore.App', version '3.1.0' was not found.

This is because you'll still need .NET Core 3.1 installed for it to work. I don't know why this wasn't fixed in the core tools template, but until it's fixed, you can use the following workaround: copying the 3.1 SDK into the image.

FROM AS installer-env
# Add the line below and it'll build successfully
COPY /usr/share/dotnet /usr/share/dotnet

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

# To enable ssh & remote debugging on app service change the base image to the one below
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
Enter fullscreen mode Exit fullscreen mode

This will hopefully get fixed soon, it's very weird to see a build error on a template like this.

Important: HTTP trigger authorization on Docker containers

If you're going to use Docker containers to run your function apps, you'll get 401 responses when testing your HTTP functions, there's a nice blog post by Martin Björkström explaining how to solve this problem, check it out!

Top comments (0)