You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core容器运行报错:无法找到libhostpolicy.so库

Fix: "libhostpolicy.so not found" Error in .NET Core Docker Container

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-contained in your dotnet publish command, 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 with dotnet 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:

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:

  1. Switch to a minimal base image (no .NET runtime included):
FROM debian:buster-slim AS base
WORKDIR /app
EXPOSE 8080
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:34:30