我有一个解决方案,其中包含作为类库的 protobuf 定义文件,以及引用 protobuf 定义文件的 gRPC 服务。它与根文件夹中的解决方案文件捆绑在一起。
如果我在 Windows 机器上构建项目,则没有问题:
$ dotnet build "GrpcService/GrpcService.csproj" -c Release -o build
MSBuild version 17.5.0+6f08c67f3 for .NET
Determining projects to restore...
Restored ...\DockerTest\GrpcService\GrpcService.csproj (in 131 ms).
GrpcService -> ...\DockerTest\build\GrpcService.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.14
构建后,我可以使用命令
dotnet .\build\GrpcService.dll
启动 gRPC 服务。
之后,我尝试在 Docker 容器内构建我的解决方案。我创建了一个如下所示的 Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:7.0.203-alpine3.17 AS build
WORKDIR /src
COPY DockerTest.sln ./
COPY Test.Grpc/*.csproj ./Test.Grpc/
COPY GrpcService/*.csproj ./GrpcService/
RUN dotnet restore
COPY . .
RUN dotnet build "GrpcService/GrpcService.csproj" -c Release -o /app/publish/backend
当我尝试在 Docker 容器内构建项目时,构建失败,因为原型构建器找不到
/src/GrpcService/GrpcService.csproj
。
$ docker build .
[+] Building 47.8s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 35B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:7.0.203-alpine3.17 0.5s
=> [1/8] FROM mcr.microsoft.com/dotnet/sdk:7.0.203-alpine3.17@sha256:626f01d39214df0611f7e4a29083447dbeb80b5f42e 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2.10kB 0.0s
=> CACHED [2/8] WORKDIR /src 0.0s
=> [3/8] COPY DockerTest.sln ./ 0.1s
=> [4/8] COPY Test.Grpc/*.csproj ./Test.Grpc/ 0.1s
=> [5/8] COPY GrpcService/*.csproj ./GrpcService/ 0.1s
=> [6/8] RUN dotnet restore 43.9s
=> [7/8] COPY . . 1.1s
=> ERROR [8/8] RUN dotnet build "GrpcService/GrpcService.csproj" -c Release -o /app/publish/backend 1.9s
------
> [8/8] RUN dotnet build "GrpcService/GrpcService.csproj" -c Release -o /app/publish/backend:
#12 0.499 MSBuild version 17.5.1+f6fdcf537 for .NET
#12 0.986 Determining projects to restore...
#12 1.493 All projects are up-to-date for restore.
#12 1.814 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: The specified task executable "/root/.nuget/packages/grpc.tools/2.49.0/tools/linux_x64/protoc" could not be run. System.ComponentModel.Win32Exception (2): An error occurred trying to start process '/root/.nuget/packages/grpc.tools/2.49.0/tools/linux_x64/protoc' with working directory '/src/GrpcService'. No such file or directory [/src/GrpcService/GrpcService.csproj]
#12 1.814 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo, String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/src/GrpcService/GrpcService.csproj]
#12 1.814 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/src/GrpcService/GrpcService.csproj]
#12 1.814 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at Microsoft.Build.Utilities.ToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/src/GrpcService/GrpcService.csproj]
#12 1.814 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at Microsoft.Build.Utilities.ToolTask.Execute() [/src/GrpcService/GrpcService.csproj]
#12 1.821
#12 1.821 Build FAILED.
#12 1.821
#12 1.821 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: The specified task executable "/root/.nuget/packages/grpc.tools/2.49.0/tools/linux_x64/protoc" could not be run. System.ComponentModel.Win32Exception (2): An error occurred trying to start process '/root/.nuget/packages/grpc.tools/2.49.0/tools/linux_x64/protoc' with working directory '/src/GrpcService'. No such file or directory [/src/GrpcService/GrpcService.csproj]
#12 1.821 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo, String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/src/GrpcService/GrpcService.csproj]
#12 1.821 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/src/GrpcService/GrpcService.csproj]
#12 1.821 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at Microsoft.Build.Utilities.ToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/src/GrpcService/GrpcService.csproj]
#12 1.821 /root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003: at Microsoft.Build.Utilities.ToolTask.Execute() [/src/GrpcService/GrpcService.csproj]
#12 1.821 0 Warning(s)
#12 1.821 1 Error(s)
#12 1.821
#12 1.821 Time Elapsed 00:00:01.13
------
executor failed running [/bin/sh -c dotnet build "GrpcService/GrpcService.csproj" -c Release -o /app/publish/backend]: exit code: 1
但是,如果我从 Dockerfile 中删除最后一条指令,创建一个容器并转储容器中的内容以查看其外观,该文件显然就在那里。
docker export -o dump.tar [container-id]
那么到底出了什么问题?
我有一段时间遇到同样的问题,这似乎是 GRPC 方面的一个已知问题。这是关于它的Github 讨论。它提到了一种解决方法,即安装自定义
glibc
库。它对我不起作用,相反我不再使用高山图像。使用默认图像就可以正常工作。