我正在使用docker运行asp.net核心3.1MVC应用程序。我能够构建映像,但是当我运行映像时,它会引发以下错误:
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。我在阅读这之后添加了以下内容
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
}
}
}
这一点也没有区别。
我试着根据<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>
发布于 2021-09-28 09:59:57
该错误是由COPY
上的错误Dockerfile
引起的。
为了调试这种错误,您应该构建从RUN dotnet build..
开始的所有行(注释),然后将映像作为
docker run -it <imagename> /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 . .
停靠器上将Shopping2
复制到/src
,找到相同的文件夹并合并所有文件,从而解决了错误。
https://stackoverflow.com/questions/66508046
复制相似问题