簡體   English   中英

嘗試在 Docker 容器內構建 C# Protobuf 項目時出現奇怪的“權限被拒絕”

[英]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 容器。 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

當我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的命令行工具不可執行
  • 基於Alpine的.net Core SDK 3.1不工作,“破壞者”一個工作

所以,這就是我所做的:

  • 基於我的構建階段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

有了這些更改,構建工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM