A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found
means dotnet YourApp.dll ran without the app's YourApp.runtimeconfig.json
beside it. In Docker that is nearly always a COPY that took only the
.dll. Copy the whole publish folder and the app starts.
Replace COPY --from=build /app/Hello.dll . with
COPY --from=build /app .. The runtimeconfig file, the deps file and the dll
all belong to the app, so the fix is to copy the whole publish folder.
The error
docker run --rm --name fix8-bld-r6 fix8-bld-p6
A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because '/app/Hello.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the '/app/Hello.runtimeconfig.json' file and specify the appropriate framework.
The container exited with code 131. Listing /app in the same image showed a
single file, Hello.dll, 4608 bytes.
Why it happens
A normal .NET app is framework-dependent: the .dll holds your code, and
Hello.runtimeconfig.json tells the dotnet host which shared
framework to load (here Microsoft.NETCore.App 10.0). When that file is missing,
the host has no framework to look up and assumes the app is self-contained, which would mean
it carries its own runtime, including libhostpolicy.so, in the app folder. It
looks in /app/, finds nothing and stops. The message says this in its last two
lines, but the first line about a missing library is the one people search for.
The runtime was installed all along; the error is about the app folder, not the image. The
same thing happens outside Docker when only the .dll is copied to a server.
The fix
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY Hello/ .
RUN dotnet publish -c Release -o /app
FROM mcr.microsoft.com/dotnet/runtime:10.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "Hello.dll"]
The only change from the broken Dockerfile is the last COPY. Run, the image
printed Hello, World! and exited with 0. If you really meant a self-contained
app, publish with a runtime identifier and --self-contained, and copy that whole
output folder instead; it then contains libhostpolicy.so itself.
How it was reproduced
A dotnet new console -n Hello project (net10.0) published in an
sdk:10.0 build stage, with a final runtime:10.0 stage that copied
only /app/Hello.dll; built with
docker build --progress=plain -t fix8-bld-p6 -f fix8-bld-p6/Dockerfile fix8-bld-ctx
and started with docker run. .NET 10.0.13 images, Docker Desktop engine 29.9.2.
Tags and folders are this test's own.
Frequently asked
- What causes libhostpolicy.so not found in Docker?
- The app's runtimeconfig.json is missing next to the dll, usually because the Dockerfile copied only the dll. The host then treats the app as self-contained and looks for libhostpolicy.so in the app folder.
- Which files does dotnet app.dll need?
- The whole publish output: the dll, app.runtimeconfig.json, app.deps.json and any dependency assemblies. Copy the publish folder, not single files.
- Do I need to install libhostpolicy.so in the image?
- No. For a framework-dependent app it comes with the runtime image. Restore the runtimeconfig.json file by copying the full publish folder and the error goes away.
More decoded errors in the Fixes category. If the build stage itself fails with "No .NET SDKs were found", see that post.