GenerateRuntimeConfigurationFiles任务意外失败

时间:2018-07-09 09:01:29

标签: c# docker .net-core dockerfile docker-windows

构建docker映像时出现不一致的错误。 如果我继续运行docker build,这将失败3次中的1次。

完整错误是:

/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: The "GenerateRuntimeConfigurationFiles" task failed unexpectedly. [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: System.IO.IOException: The process cannot access the file '/app/MyProject.Api/bin/Release/netcoreapp2.1/MyProject.Api.runtimeconfig.json' because it is being used by another process. [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at System.IO.FileStream.Init(FileMode mode, FileShare share) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at System.IO.File.Create(String path) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at Microsoft.NET.Build.Tasks.GenerateRuntimeConfigurationFiles.WriteToJsonFile(String fileName, Object value) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at Microsoft.NET.Build.Tasks.GenerateRuntimeConfigurationFiles.WriteRuntimeConfig(ProjectContext projectContext) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at Microsoft.NET.Build.Tasks.GenerateRuntimeConfigurationFiles.ExecuteCore() [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at Microsoft.NET.Build.Tasks.TaskBase.Execute() [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/app/MyProject.Api/MyProject.Api.csproj]
  MyProject.Api -> /app/MyProject.Api/bin/Release/netcoreapp2.1/MyProject.Api.dll
  MyProject.Api -> /app/MyProject.Api/out/
The command '/bin/sh -c ps -ef && dotnet publish --configuration Release --no-restore --framework netcoreapp2.1 --output out' returned a non-zero code: 1

这行让我感到困扰的是,该进程无法访问文件'/app/MyProject.Api/bin/Release/netcoreapp2.1/MyProject.Api.runtimeconfig.json',因为该文件正在被另一个进程使用。 [/app/MyProject.Api/MyProject.Api.csproj]

还有什么其他过程可以锁定文件?

构建过程没有失败时,每次都会显示此警告:

/usr/share/dotnet/sdk/2.1.301/Microsoft.Common.CurrentVersion.targets(4560,5): warning MSB3026: Could not copy "/app/MyProject.Api/Keys/public.pem" to "bin/Release/netcoreapp2.1/Keys/public.pem". Beginning retry 1 in 1000ms. The process cannot access the file '/app/MyProject.Api/bin/Release/netcoreapp2.1/Keys/public.pem' because it is being used by another process.  [/app/MyProject.Api/MyProject.Api.csproj]

哪个看起来像相同的问题,但可以单独解决,然后生成图像,并且public.pem文件已正确复制。

我的dockerfile:

FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app

COPY . ./

RUN dotnet restore --source https://api.nuget.org/v3/index.json

RUN dotnet publish --configuration Release --no-restore --framework netcoreapp2.1 --output out

FROM microsoft/dotnet:2.1.1-aspnetcore-runtime as runtime
WORKDIR /app

COPY --from=build-env /app/MyProject.Api/out .
ENTRYPOINT ["dotnet", "MyProject.Api.dll"]

,我像这样运行docker build: docker build --no-cache -t myproject:test -f Dockerfile。

我发现在github上发布的唯一类似的问题是在大多数情况下都可以持续工作的持续失败,但是经常失败,而且我仍然不明白为什么。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以在Linux上使用 fuser [使用的文件]

在MacOS上

lsof [已使用文件]

查找该进程的pid。

答案 1 :(得分:0)

我认为Dockerfile中的某些命令需要更改。

FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:2.1.1-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyProject.Api.dll"]

参考文献:

  1. https://docs.docker.com/engine/examples/dotnetcore/#prerequisites

  2. https://raw.githubusercontent.com/dotnet/dotnet-docker/master/samples/dotnetapp/Dockerfile