Running .Net Core based Azure Function in Docker Container on Apple Silicon
Asked Answered
S

3

8

I am trying to run a .Net Core based Azure Function inside a Docker container on a M1 Macbook without success so far.

Originally I used the Azure Function Core Tools CLI to create the function with the following command: func init LocalFunctionsProject --worker-runtime dotnet --docker which created the following Dockerfile.

FROM mcr.microsoft.com/dotnet/core/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

# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice
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"]

Building the Docker image and running as Container works just fine on a amd based machine. In the meanwhile I got myself a M1 Macbook on which running the Azure Function inside a Docker Container does not work anymore. It gives me the following exception:

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
Unhandled exception. System.IO.IOException: Function not implemented
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String filter)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.<.ctor>b__1_0()
   at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1..ctor(Func`1 changeTokenProducer, Action`1 changeTokenConsumer, TState state)
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.Azure.WebJobs.Script.WebHost.Program.BuildWebHost(String[] args) in /src/azure-functions-host/src/WebJobs.Script.WebHost/Program.cs:line 35
   at Microsoft.Azure.WebJobs.Script.WebHost.Program.Main(String[] args) in /src/azure-functions-host/src/WebJobs.Script.WebHost/Program.cs:line 25
qemu: uncaught target signal 6 (Aborted) - core dumped

What I tried so far

  1. Forcing Docker to use amd based base image by adding --platform=linux/amd64 to the FROM section of the first stage in the Dockerfile which gives me the following error message:
> [installer-env 3/3] RUN cd /src/dotnet-function-app &&     mkdir -p /home/site/wwwroot &&     dotnet publish *.csproj --output /home/site/wwwroot:                               
#9 3.258 Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
#9 3.258 Copyright (C) Microsoft Corporation. All rights reserved.
#9 3.258 
#9 3.441 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
#9 3.456 Segmentation fault
------
executor failed running [/bin/sh -c cd /src/dotnet-function-app &&     mkdir -p /home/site/wwwroot &&     dotnet publish *.csproj --output /home/site/wwwroot]: exit code: 139
  1. Changing the base image of the second stage to mcr.microsoft.com/azure-functions/dotnet:3.0-arm32v7 which gave me the following error:
WARNING: The requested image's platform (linux/arm) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
A fatal error occurred, the folder [/usr/share/dotnet/host/fxr] does not contain any version-numbered child folders

Conclusion

My personal conclusion from how I understand the problem is that probably it will not work on my M1 machine unless there is a dedicated azure-function/dotnet image for arm64 v8 machines. If I am completely wrong, please hint me to the right direction.

Stanislaus answered 17/4, 2021 at 11:4 Comment(1)
This is also the case for arm64v8 based Raspberry Pi devices, Did you get this working in the end?Mistrial
C
0

You are correct. Unfortunately, the M1 architecture is not supported in your issue's scope. Please see this thread on GitHub and feel free to upvote it.

Carpospore answered 8/4, 2023 at 21:42 Comment(0)
S
0

Issam Ben documented the instructions for running dotnet containers on M1/M2 macs:

  • Update docker to >= 4.16.2
  • Enable Use Rosetta for x86/amd64 emulation on Apple Silicon option in Settings > Features in development
  • Set platform to linux/amd64 when running docker build e.g. docker build --platform linux/amd64 ...

(edited quote for clarity)

I've used these settings to successfully run dotnet containers on my M1 mac.

Study answered 22/8, 2023 at 21:9 Comment(0)
N
0

I'm successfully running a .NET 8 Isolated Azure Functions host in Docker on an ARM64v8 device. A couple of caveats first:

  • It's not an official image - someone has kindly built these in their spare time, so use with caution.
  • I'm using Raspberry Pi rather than Apple Silicon, but I think the ARM instruction set is identical.
  • I've been running this continuously for several years (as .NET 7 and then .NET 8) as a personal project at home; but not for something in production or where I've got security concerns.

The image is from https://hub.docker.com/r/mohsinonxrm/azure-functions-dotnet

My Dockerfile looks like this:

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

COPY --from=mcr.microsoft.com/dotnet/sdk:8.0 /usr/share/dotnet /usr/share/dotnet

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

FROM mohsinonxrm/azure-functions-dotnet:4-isolated8.0-arm64v8

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    TZ=Europe/London \
    LC_ALL=en_GB.UTF-8 \
    AzureWebJobsStorage=DefaultEndpointsProtocol=https;AccountName=Your-Account;AccountKey=Your-Key;EndpointSuffix=core.windows.net

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

My accompanying Docker Compose file accordingly has the platform as:

    platform: linux/arm64/v8
Nepenthe answered 18/8 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.