【问题标题】:Docker build error - NuGet.targets : error : Access to the path is deniedDocker 构建错误 - NuGet.targets:错误:访问路径被拒绝
【发布时间】:2020-01-02 20:52:06
【问题描述】:

我有一个多阶段构建的 Dockerfile(大部分文件是由 VS 生成的)。当我通过 Docker 在我的 Windows 10 机器上构建我的应用程序时,一切正常。

当我尝试在 Windows 2019 Standard 服务器上构建时,我收到以下错误:

    Step 13/20 : WORKDIR "/src/Dining.Api"
     ---> Using cache
     ---> 63b85c1e029d
    Step 14/20 : RUN dotnet build "Dining.Api.csproj" -c Release -o /app
     ---> Running in 02a2e5f36cac
    Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.

    C:\Program Files\dotnet\sdk\2.2.401\NuGet.targets(123,5): error : Access to the path 'C:\src\Dining.Domain\obj\Dining.Domain.csproj.nuget.dgspec.json' is denied. [C:\src\Dining.Api\Dining.Api.csproj]

    Build FAILED.

    C:\Program Files\dotnet\sdk\2.2.401\NuGet.targets(123,5): error : Access to the path 'C:\src\Dining.Domain\obj\Dining.Domain.csproj.nuget.dgspec.json' is denied. [C:\src\Dining.Api\Dining.Api.csproj]
        0 Warning(s)
        1 Error(s)

    Time Elapsed 00:00:05.06
    The command 'cmd /S /C dotnet build "Dining.Api.csproj" -c Release -o /app' returned a non-zero code: 1

当我在我的 Windows 10 机器上构建时,这里是 docker build 中同一步骤的输出:

Step 13/20 : WORKDIR "/src/Dining.Api"
 ---> Running in e7be7be7e992
Removing intermediate container e7be7be7e992
 ---> 265b7c6ade96
Step 14/20 : RUN dotnet build "Dining.Api.csproj" -c Release -o /app
 ---> Running in 9c0d92e0df82
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 440.05 ms for /src/Dining.DataAccess/Dining.DataAccess.csproj.
  Restore completed in 43.97 ms for /src/Dining.Domain/Dining.Domain.csproj.
  Restore completed in 62.16 ms for /src/Dining.Service/Dining.Service.csproj.
  Restore completed in 1.1 sec for /src/Dining.Api/Dining.Api.csproj.
  Dining.Domain -> /app/Dining.Domain.dll
  Dining.DataAccess -> /app/Dining.DataAccess.dll
  Dining.Service -> /app/Dining.Service.dll
  Dining.Api -> /app/Dining.Api.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

我正在我的机器和 Windows Server 上运行 docker 版本 19.03.1。这是docker文件:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Dining.Api/Dining.Api.csproj", "Dining.Api/"]
COPY ["Dining.Domain/Dining.Domain.csproj", "Dining.Domain/"]
COPY ["Dining.DataAccess/Dining.DataAccess.csproj", "Dining.DataAccess/"]
COPY ["Dining.Service/Dining.Service.csproj", "Dining.Service/"]
COPY ["NuGet.Config", "./"]
RUN dotnet restore "Dining.Api/Dining.Api.csproj"
COPY . .
WORKDIR "/src/Dining.Api"
RUN dotnet build "Dining.Api.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Dining.Api.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Dining.Api.dll", "--server.urls", "http://*:80"]

我研究并发现了这个:https://github.com/NuGet/Home/issues/7836 它提供了很多关于错误的细节,但并没有真正提供解决方案。我尝试使用 .dockerignore 排除复制 **/obj/ 目录中的文件,但问题仍然存在。有没有人遇到过这个问题并提出解决方案/解决方法?

【问题讨论】:

  • 作为后续,我能够解决这个问题。在我原来的帖子中,我提到我尝试创建一个 .dockerignore 但我说那没有用。我刚刚意识到我将 .dockerignore 放在项目文件夹中,而不是在解决方案的根目录中。当我将 .dockerignore 添加到解决方案根目录,然后添加 **/obj/ 行时,我能够在 Windows Server 2019 上构建映像。
  • 我认为问题在于您在本地使用 Linux 容器,但在服务器上使用 Windows 容器。如果您要部署到 Windows,也可以在开发中使用 Windows 容器。如果您切换,您将遇到同样的问题:您的开发构建将失败。然后,只需修改您的 Dockerfile 以适用于开发中的 Windows 容器,您就可以在服务器上运行了。
  • 感谢克里斯的回复。在我的开发机器上,我将 docker 从 Linux 切换到 Windows,但没有收到错误消息。我只在 Windows 2019 Standard 服务器上看到错误。您提到修改 Dockerfile 以适用于 Windows 容器 - 您能解释一下如何做到这一点吗?我的印象是 aspnetcore-runtime 和 sdk 映像可以在 Windows 和 Linux 上运行。
  • 确实如此。问题是你的路径。

标签: docker asp.net-core windows-server-2019


【解决方案1】:

我设法通过添加解决了类似的问题

USER Administrator

到 docker 文件,如:DotNet 6 fails to run in a Windows container: Permission denied

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2013-07-10
    相关资源
    最近更新 更多