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