尝试在 Docker 容器中构建 C# Protobuf 项目时出现奇怪的问题 "Permission denied"

Strange "Permission denied" when trying to build C# Protobuf project inside Docker container

我有一个相当简单的 .net Core C# 项目,其中有一个 .proto 文件。 .csproj 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RootNamespace>My.Overview</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <Protobuf Include="..\..\Protos\overview.proto" GrpcServices="Both" Link="Protos\overview.proto" />
  </ItemGroup>
  <Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>

我在一个 ASP.net Core 3.1 项目中引用了这个,我使用 Paket 来解决依赖关系。

当我从 Visual Studio 或 dotnet build 编译它时一切正常。

不过我还想将服务构建为 Docker 容器。 Docker文件的相关部分如下所示:

RUN dotnet tool install -g paket
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.dotnet/tools

COPY paket.dependencies /build/
COPY paket.lock /build/
WORKDIR /build
RUN paket install
COPY . /build
RUN dotnet publish -c Release AppThatUses.My.Overview

当我 docker build 时,我得到这个:

/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003: The specified task executable "/root/.nuget/packages/grpc.tools/2.29.0/tools/linux_x64/protoc" could not be run. System.ComponentModel.Win32Exception (13): Permission denied [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at System.Diagnostics.Process.Start() [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at Microsoft.Build.Utilities.ToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at Microsoft.Build.Utilities.ToolTask.Execute() [/build/Model/Overview/Overview.csproj]
The command '/bin/sh -c dotnet publish -c Release AppThatUses.My.Overview' returned a non-zero code: 1

我对此进行了调查,发现 protoc 不可执行,所以我在 dotnet publish:

之前添加了这一行
RUN find /root/.nuget/packages -name protoc |grep linux |xargs chmod +x

现在,我得到:

/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003: The specified task executable "/root/.nuget/packages/grpc.tools/2.29.0/tools/linux_x64/protoc" could not be run. System.ComponentModel.Win32Exception (2): No such file or directory [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at System.Diagnostics.Process.Start() [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at Microsoft.Build.Utilities.ToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/build/Model/Overview/Overview.csproj]
/root/.nuget/packages/grpc.tools/2.29.0/build/_protobuf/Google.Protobuf.Tools.targets(264,5): error MSB6003:    at Microsoft.Build.Utilities.ToolTask.Execute() [/build/Model/Overview/Overview.csproj]

我三重检查 /build/Model/Overview/Overview.csproj 确实存在。

我错过了什么?

不是错误修复,而是解决方法:

  • 由于某些原因,通过 dotnet tool install 安装的命令行工具不可执行
  • .net Core SDK 3.1 基于 Alpine 不工作,"buster" 一个工作

所以,这就是我所做的:

  • 我的构建阶段基于 mcr.microsoft.com/dotnet/core/sdk:3.1-buster 而不是 mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
  • 在我的 dotnet build 之前添加了以下行: RUN chmod -Rf +x /root/.nuget/packages/grpc.tools/2.29.0/tools

完成这些更改后,构建工作正常。