【问题标题】:The library 'libhostpolicy.so' required to execute the application was not found in '/app/bin/Debug/netcoreapp3.1/'在“/app/bin/Debug/netcoreapp3.1/”中找不到执行应用程序所需的库“libhostpolicy.so”
【发布时间】:2021-06-05 01:10:42
【问题描述】:

我正在使用 docker 运行 asp.net core 3.1 MVC 应用程序。 我能够构建图像,但是当我运行图像时,它会引发以下错误:

A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/bin/Debug/netcoreapp3.1/'.
Failed to run as a self-contained app. If this should be a framework-dependent app, add the /app/bin/Debug/netcoreapp3.1/Shopping2.runtimeconfig.json file specifying the appropriate framework.

Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Shopping2.csproj", "Shopping2/"]
RUN dotnet restore "Shopping2/Shopping2.csproj"
COPY . .
WORKDIR "/src/Shopping2"
RUN dotnet build "Shopping2.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Shopping2.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Shopping2.dll"]

我不太确定如何使用 runtimeconfig.json。 我在阅读this后添加了以下内容@

Shopping2.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "netcoreapp3.1",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "3.1.0"
    },
    "configProperties": {
      "System.GC.Concurrent": false,
      "System.Threading.ThreadPool.MinThreads": 4,
      "System.Threading.ThreadPool.MaxThreads": 25
    }
  }
}

根本没有区别。

我尝试根据this SO answer 添加<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> ,但随后它开始给我一个不同的错误:

Unhandled exception. System.MissingMethodException: Entry point not found in assembly 'Shopping2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

这是应用程序设置的屏幕截图

以下是我的.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> // adding this changed the error
    <StartupObject></StartupObject>
    <OutputType>Library</OutputType>
    <LangVersion>latest</LangVersion>

  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
  </ItemGroup>

  <ItemGroup>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

</Project>

【问题讨论】:

  • 在 Linux 容器上安装库非常棘手,您可能必须搜索库以了解如何安装它们,然后重试运行您的应用程序,如果出现问题(通常可能缺少另一个依赖项),重复该步骤,直到运行正常。最后可能会安装一些不必要的库,但没有太大的危害。
  • 您是否尝试将&lt;OutputType&gt;Library&lt;/OutputType&gt; 更改为&lt;OutputType&gt;Exe&lt;/OutputType&gt;,如GitHub issue 中所述?
  • @PavelAnikhouski 是的,。我尝试了所有三个可能的值。库、Exe 和控制台。但没有运气。我终于设法通过将 Dockerfile 移到解决方案文件所在的位置来使其工作。早些时候,它与 Visual Studio 添加的项目文件处于同一级别。现在我能够构建和运行图像,但我不能再使用 Visual Studio 的 run as docker 功能。我必须选择项目或 IIS Express。

标签: c# visual-studio docker asp.net-core dockerfile


【解决方案1】:

该错误是由Dockerfile 上的错误COPY 引起的。

要调试此类错误,您应该构建 docker 映像,删除(注释)从 RUN dotnet build.. 及以下开始的所有行,然后启动映像

docker run -it &lt;imagename&gt; /bin/sh

在提示符下运行ls,您应该会看到与预期不同的文件夹树。

在这个特殊情况下

WORKDIR /src
# cd /src (create if not exist)

COPY ["Shopping2.csproj", "Shopping2/"] 
# Copy the csproj is in /src/Shopping2/Shopping.csproj

RUN dotnet restore "Shopping2/Shopping2.csproj"
# Restore in the /src/Shopping2/ folder

COPY . .
# THE ERROR!
# The content of the Docker run command folder is copied into /src
# probably on the docker image we have 
# /src/obj
# /src/bin
# /src/<all sources>
# /src/Shopping2/shopping2.csproj

WORKDIR "/src/Shopping2"
# cd to /src/Shopping2

RUN dotnet build "Shopping2.csproj" -c Release -o /app/build
# The error on build
# The compiler found ONLY the .csproj and the restored packages, it don't find the source code

作者通过将Dockerfile移到源文件夹外并在命令COPY . . docker将Shopping2复制到/src来解决错误,它找到了相同的文件夹并全部合并。

【讨论】:

    猜你喜欢
    • 2018-11-09
    • 2022-10-19
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多