.NET Core容器运行报错:无法找到libhostpolicy.so库
Hey there, let's break down why you're hitting this error and how to fix it quickly.
The root cause here is a mismatch between your publish configuration and how you're trying to run the app:
- You're using
--self-containedin yourdotnet publishcommand, which builds a package that includes the entire .NET Core runtime. - But your final base image is
mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim, which already has the .NET runtime installed, and you're trying to start the app withdotnet WebApplication1.dll—this combination causes the runtime to look for files that don't exist in a self-contained publish.
Here are two solid solutions depending on your needs:
Solution 1: Use Framework-Dependent Publish (Recommended)
Since you're already using the official ASP.NET Core runtime base image, you don't need to include the runtime in your publish. This keeps your image smaller and leverages the optimized runtime from Microsoft.
Modify the publish stage in your Dockerfile to remove the --self-contained flag:
FROM build AS publish RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
Keep the rest of your Dockerfile the same—your existing ENTRYPOINT ["dotnet", "WebApplication1.dll"] will work perfectly with this setup.
Solution 2: Stick with Self-Contained Publish (If You Need It)
If you really want a self-contained app (no external runtime dependency), you'll need to adjust two parts of your Dockerfile:
- Switch to a minimal base image (no .NET runtime included):
FROM debian:buster-slim AS base WORKDIR /app EXPOSE 8080
- Update the publish command to target Linux (ensure compatibility with the base image) and adjust the entrypoint to run the native executable:
FROM build AS publish RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish --self-contained -r linux-x64 FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["./WebApplication1"]
(Note: The executable name matches your project name and has no .dll suffix on Linux.)
Either of these fixes should resolve the libhostpolicy.so not found error. Solution 1 is the more common approach for .NET Docker deployments since it's more efficient.
内容的提问来源于stack exchange,提问作者SexyMF

