【问题标题】:Dockerfile fails because I cannot copy the class library it depends onDockerfile失败,因为我无法复制它所依赖的类库
【发布时间】:2023-02-12 20:38:09
【问题描述】:

我正在尝试从 Dockerfile 构建图像。问题是它无法找到消息类库,这意味着我必须执行类似 COPY ["src/MarketData.Subscriber/MarketData.Subscriber.csproj", "MarketData.Subscriber/"] COPY ["src/MarketData.Messages/MarketData.Messages.csproj", "MarketData.Messages/"] 的操作,但是当我在 WORKDIR 下添加这些时,COPY 失败。我该如何解决?

文件夹结构

 .
 +---src
 |   +---MarketData.Api
 |   +---MarketData.Messages
 |   +---MarketData.Subscriber
 |       +---Dockerfile
 +---MarketData.sln

文件

# Stage 1 - Build
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder

WORKDIR /app/src

# Restore
COPY *.csproj .
RUN dotnet restore -r linux-x64

# Build
COPY . .
RUN dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore

# Stage 2 - Publish
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app

RUN addgroup --system --gid 101 app \
    && adduser --system --ingroup app --uid 101 app


COPY --from=builder --chown=app:app /app/publish .

USER app
ENTRYPOINT ["./MarketData.Subscriber"]

错误

[+] Building 4.0s (14/15)
 => [internal] load build definition from Dockerfile                                                                                                                                                        0.0s
 => => transferring dockerfile: 589B                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                           0.0s
 => => transferring context: 35B                                                                                                                                                                            0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:7.0                                                                                                                                           0.4s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:7.0                                                                                                                                        0.0s
 => [stage-1 1/4] FROM mcr.microsoft.com/dotnet/aspnet:7.0                                                                                                                                                  0.0s
 => [builder 1/6] FROM mcr.microsoft.com/dotnet/sdk:7.0@sha256:66a16cd71b82a2df577e52dfd01d8039ed4c20e3f17fca31b2fb3cf5929eaa7f                                                                             0.0s
 => [internal] load build context                                                                                                                                                                           0.0s
 => => transferring context: 650B                                                                                                                                                                           0.0s
 => CACHED [stage-1 2/4] WORKDIR /app                                                                                                                                                                       0.0s
 => CACHED [stage-1 3/4] RUN addgroup --system --gid 101 app     && adduser --system --ingroup app --uid 101 app                                                                                            0.0s
 => CACHED [builder 2/6] WORKDIR /app/src                                                                                                                                                                   0.0s
 => CACHED [builder 3/6] COPY *.csproj .                                                                                                                                                                    0.0s
 => CACHED [builder 4/6] RUN dotnet restore -r linux-x64                                                                                                                                                    0.0s
 => CACHED [builder 5/6] COPY . .                                                                                                                                                                           0.0s
 => ERROR [builder 6/6] RUN dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore                                                                                         3.5s
------
 > [builder 6/6] RUN dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore:
#14 0.567 MSBuild version 17.4.0-preview-22368-02+c8492483a for .NET
#14 1.270 /usr/share/dotnet/sdk/7.0.100-preview.7.22377.5/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(219,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [/app/src/MarketData.Subscriber.csproj]
#14 1.532 /usr/share/dotnet/sdk/7.0.100-preview.7.22377.5/Microsoft.Common.CurrentVersion.targets(2082,5): warning : The referenced project '../MarketData.Messages/MarketData.Messages.csproj' does not exist. [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(2,7): error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(3,18): error CS0234: The type or namespace name 'Messages' does not exist in the namespace 'MarketData' (are you missing an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(5,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(6,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/ActorSystemConfiguration.cs(1,18): error CS0234: The type or namespace name 'Messages' does not exist in the namespace 'MarketData' (are you missing an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/ActorSystemConfiguration.cs(3,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/ActorSystemConfiguration.cs(4,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(5,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(6,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(7,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(8,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(9,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(1,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(2,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(10,39): error CS0246: The type or namespace name 'FtxSubscriptionBase' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(12,22): error CS0246: The type or namespace name 'ClusterIdentity' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(18,9): error CS0246: The type or namespace name 'IContext' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(19,9): error CS0246: The type or namespace name 'ClusterIdentity' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(8,22): error CS0246: The type or namespace name 'ActorSystem' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(11,37): error CS0246: The type or namespace name 'ActorSystem' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(36,36): error CS0246: The type or namespace name 'SubscriptionRequest' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(51,38): error CS0246: The type or namespace name 'SubscriptionRequest' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(57,26): error CS0246: The type or namespace name 'SubscriptionStatus' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
------
executor failed running [/bin/sh -c dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore]: exit code: 1

【问题讨论】:

    标签: c# .net dockerfile asp.net-core-webapi


    【解决方案1】:

    将您的 dockerfile 保持在解决方案级别。那么只有它才能找到 src 文件夹。

    你的文件夹结构应该是这样的。

    .
     +---src
     |   +---MarketData.Api
     |   +---MarketData.Messages
     |   +---MarketData.Subscriber
     +---MarketData.sln
     +---Dockerfile
    

    并将相应的复制项目文件命令添加到 dockerfile。

    希望能帮助到你。

    【讨论】:

    • 感谢你的回答!我见过几个微服务项目,包括来自 Microsoft github.com/dotnet-architecture/eShopOnContainers 的项目,它们确实在 src/ProjectName 文件夹中有它们的 Dockerfile。他们是如何做到的?
    • 我猜他们正在使用 docker-compose 构建图像,在这种情况下,构建上下文设置在更高级别。你也可以试试看。如果你的目标是最终使用 docker-compose。
    • `=> 错误 [builder 4/6] RUN dotnet restore -r linux-x64 0.7s ------ > [builder 4/6] RUN dotnet restore -r linux-x64: #12 0.622 MSBUILD: 错误 MSB1003:指定项目或解决方案文件。当前工作目录不包含项目或解决方案文件。 ------ 执行程序运行失败 [/bin/sh -c dotnet restore -r linux-x64]:退出代码:1`
    • 我假设我需要在将 Dockerfile 移动到 .sln 附近时构建解决方案?
    • 您需要在还原步骤中指定主项目,它将在内部还原所有其他依赖项目。
    【解决方案2】:

    感谢 Cisien,问题已解决。这是由于我如何运行构建。

    它应该从 root 和 docker build . -f src/Dockerfile 运行。这 '。'表示构建上下文,或者换句话说,表示可用于 docker 构建过程的父目录。

    C:UsersuserDesktopMarketData>docker build -t marketdata:1.0 -f ./src/MarketData.Subscriber/Dockerfile 。

    希望它能帮助别人。

    文件

    # Stage 1 - Build
    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder
    
    WORKDIR /app/src
    
    # Restore
    COPY src/MarketData.Messages/MarketData.Messages.csproj MarketData.Messages/
    COPY *.csproj .
    RUN dotnet restore
    
    # Build
    COPY . .
    RUN dotnet publish -c Release -o /app/publish --no-self-contained --no-restore
    
    # Stage 2 - Publish
    FROM mcr.microsoft.com/dotnet/aspnet:7.0
    WORKDIR /app
    
    RUN addgroup --system --gid 101 app 
        && adduser --system --ingroup app --uid 101 app
    
    COPY --from=builder --chown=app:app /app/publish .
    
    USER app
    ENTRYPOINT ["./MarketData.Subscriber"]
    
    

    【讨论】:

      【解决方案3】:

      在visual studio中,你可以将docker文件移动到一个后台文件夹中。 例如,folder1/folder2/dockerfile 移动到 folder1/dockerfile 并构建图像。

      Docker build -t imagename .
      
      Docker run -p 7778:80 imagename
      

      这个对我有用。

      【讨论】:

        猜你喜欢
        • 2011-04-08
        • 2019-03-20
        • 2019-12-30
        • 1970-01-01
        • 1970-01-01
        • 2020-10-15
        • 1970-01-01
        • 2015-11-05
        • 2023-01-04
        相关资源
        最近更新 更多