'Unable to load shared library 'kernel32.dll'

I'm running a dotnet webapp in my local (windows machine) and it works just fine. When I deploy the same application to an AKS container and try running it, it fails with

System.TypeInitializationException: The type initializer for 'System.Runtime.Caching.MemoryMonitor' threw an exception.
       ---> System.DllNotFoundException: Unable to load shared library 'kernel32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libkernel32.dll: cannot open shared object file: No such file or directory
         at Interop.Kernel32.GlobalMemoryStatusEx(MEMORYSTATUSEX& lpBuffer)
         at System.Runtime.Caching.MemoryMonitor..cctor()
         --- End of inner exception stack trace ---

Below is my Docker

FROM mcr.microsoft.com/dotnet/aspnet:5.0-bullseye-slim
RUN apt-get update && apt-get install -y \
curl
WORKDIR /app
EXPOSE 80
EXPOSE 443
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]

Let me know if anyone has solved or seen this before



Solution 1:[1]

It looks like you're copying in your binaries directly into the container. The tag you are using is 5.0-bullseye-slim is Linux-based. So if the binaries you're copying in were built for the Windows platform, which I'm guessing they are since it's looking for kernel32.dll, then that's not going to run in a Linux environment.

You'll either need to build your binaries so that they are targeting Linux, which can be done with dotnet build -r linux-x64 ..., or a better option would be to build your project as part of the Dockerfile.

Here's an example Dockerfile that demonstrates this:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore

# copy and publish app and libraries
COPY . .
RUN dotnet publish -c release -o /app --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "dotnetapp.dll"]

(Taken from https://github.com/dotnet/dotnet-docker/blob/0fc0e2c6af6303cfd4676f1ac8c21090d82b0072/samples/dotnetapp/Dockerfile)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Thalman