简体   繁体   English

尝试在 Docker 容器内构建 C# Protobuf 项目时出现奇怪的“权限被拒绝”

[英]Strange “Permission denied” when trying to build C# Protobuf project inside Docker container

I have a fairly simple .net Core C# project with a .proto file.我有一个相当简单的 .net Core C# 项目,带有.proto文件。 The .csproj looks like this: .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>

I have referenced this in an ASP.net Core 3.1 project, and I use Paket for resolution of dependencies.我在 ASP.net Core 3.1 项目中引用了这个,我使用 Paket 来解决依赖关系。

When I compile this from Visual Studio or dotnet build everything works fine.当我从 Visual Studio 或dotnet build编译时,一切正常。

However I also want to build the service as a Docker container.但是,我还想将服务构建为 Docker 容器。 The relevant portion of the Dockerfile looks like this: Dockerfile 的相关部分如下所示:

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

When I docker build , I get this:当我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

I investigated this and I saw that protoc was not executable, so I added this line just before the dotnet publish :我对此进行了调查,发现protoc不可执行,因此我在dotnet publish之前添加了这一行:

RUN find /root/.nuget/packages -name protoc |grep linux |xargs chmod +x

Now, I get:现在,我得到:

/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]

I triple-checked that /build/Model/Overview/Overview.csproj is actually there.我三重检查/build/Model/Overview/Overview.csproj确实存在。

What am I missing?我错过了什么?

Not a bugfix but a workaround:不是错误修复,而是一种解决方法:

  • For some reason, command-line tools installed via dotnet tool install are not executable由于某种原因,通过dotnet tool install的命令行工具不可执行
  • .net Core SDK 3.1 based on Alpine does not work, the "buster" one works基于Alpine的.net Core SDK 3.1不工作,“破坏者”一个工作

So, here's what I did:所以,这就是我所做的:

  • Based my build stage on mcr.microsoft.com/dotnet/core/sdk:3.1-buster instead of mcr.microsoft.com/dotnet/core/sdk:3.1-alpine基于我的构建阶段mcr.microsoft.com/dotnet/core/sdk:3.1-buster而不是mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
  • Added the following line right before my dotnet build : RUN chmod -Rf +x /root/.nuget/packages/grpc.tools/2.29.0/tools在我的dotnet build之前添加了以下行: RUN chmod -Rf +x /root/.nuget/packages/grpc.tools/2.29.0/tools

With these changes in place, the build works.有了这些更改,构建工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM