0

下面是 Visual Studio 2022 创建的自动生成的 Dockerfile。我可以在 Docker 中调试应用程序,但无法创建 Docker 映像。每次我尝试时,我都会在底部复制错误,说它无法复制文件。相对路径不正确吗?

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src


COPY ["ServicePM/ServicePM.csproj", "ServicePM/"]
COPY ["../Common/MyCompany.Common/MyCompany.Common.csproj", "../Common/MyCompany.Common/"]
COPY ["../Common/MyCompany.Common.Utilities/MyCompany.Common.Utilities.csproj", "../Common/MyCompany.Common.Utilities/"]
COPY ["../BcEventHarvest/MyCompany.PartitionUpdater/MyCompany.PartitionUpdater.csproj", "../BcEventHarvest/MyCompany.PartitionUpdater/"]
COPY ["../Common/MyCompany.Data2/MyCompany.Data2.csproj", "../Common/MyCompany.Data2/"]
COPY ["../Common/MyCompany.EventCache/MyCompany.EventCache/MyCompany.EventCache.csproj", "../Common/MyCompany.EventCache/MyCompany.EventCache/"]
COPY ["../Common/MyCompany.CloudStorage/MyCompany.CloudStorage.csproj", "../Common/MyCompany.CloudStorage/"]
COPY ["../Common/Providers/CSProfiler/MyCompany.Common.Providers.CSProfiler.csproj", "../Common/Providers/CSProfiler/"]
COPY ["../Common/MyCompany.EventDataMesh/MyCompany.EventDataMesh.csproj", "../Common/MyCompany.EventDataMesh/"]
COPY ["../BcEventHarvest/BcEventHarvest/BcEventHarvest.csproj", "../BcEventHarvest/BcEventHarvest/"]
COPY ["../Common/Providers/DownloadManagement/MyCompany.Common.Providers.DownloadManagement.csproj", "../Common/Providers/DownloadManagement/"]
COPY ["../Common/Providers/ImageManagement/MyCompany.Common.Providers.ImageManagement.csproj", "../Common/Providers/ImageManagement/"]
COPY ["../Common/Providers/WebDriver/MyCompany.Common.Providers.WebDriver.csproj", "../Common/Providers/WebDriver/"]
COPY ["../BcEventHarvest/MyCompany.FileCache/MyCompany.FileCache.csproj", "../BcEventHarvest/MyCompany.FileCache/"]
COPY ["../Common/MyCompany.Data/MyCompany.Data.csproj", "../Common/MyCompany.Data/"]
RUN dotnet restore "ServicePM/ServicePM.csproj"
COPY . .
WORKDIR "/src/ServicePM"
RUN dotnet build "ServicePM.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ServicePM.dll"]

这是错误

15>COPY failed: failed to resolve scoped path ..\Common\CitySpark.Common\CitySpark.Common.csproj (): evalSymlinksInScope: \\?\C:\ProgramData\Docker\tmp\Common\CitySpark.Common\CitySpark.Common.csproj is not in \\?\C:\ProgramData\Docker\tmp\docker-builder739514268. Possible cause is a forbidden path outside the build context
15>C:\dev\MyCompany\PartitionManager\ServicePM\Dockerfile : error CTC1014: Docker command failed with exit code 1.
15>C:\dev\MyCompany\PartitionManager\ServicePM\Dockerfile : error CTC1014: COPY failed: failed to resolve scoped path ..\Common\MyCompany.Common\MyCompany.Common.csproj (): evalSymlinksInScope: \\?\C:\ProgramData\Docker\tmp\Common\MyCompany.Common\MyCompany.Common.csproj is not in \\?\C:\ProgramData\Docker\tmp\docker-builder739514268. Possible cause is a forbidden path outside the build context
15>Done building project "ServicePM.csproj" -- FAILED.
========== Rebuild All: 14 succeeded, 1 failed, 0 skipped ==========```
4

1 回答 1

0

我是负责 Visual Studio 中 Docker 支持的团队的开发人员。

看起来您的项目有一些依赖项位于比项目的解决方案文件更高的文件夹中,这是当前不支持的方案。构建 Dockerfile 时,必须指定一个上下文文件夹,该文件夹用于解析对复制到生成的映像中的文件或文件夹的引用。无法解析此上下文文件夹上方的任何路径(例如,以 .. 开头)。对于 Visual Studio,我们已决定使用包含打开的解决方案文件 (.sln) 的文件夹作为构建上下文路径。

使用 docker build,重要的是不要意外使用构建的高级上下文文件夹,因为 Dockerfiles 中的常见模式是将上下文下的所有文件和文件夹复制到带有“COPY ...”的图像中。命令。这意味着我们不能只将构建上下文设置为包含所有引用项目的最高文件夹,因为我们可能会意外地将与给定解决方案(甚至系统文件)无关的文件复制到新的 Docker 映像中。

我们将在未来的版本中为 Docker 脚手架逻辑添加一些额外的检查,以更主动地捕捉这种情况并提供有用的错误消息,而不是搭建损坏的 Dockerfile。

对于此项目,您需要将解决方案文件移动到包含 ServicePM 项目以及所有引用项目的父文件夹。不幸的是,如果父文件夹包含其他不相关的项目/文件,则可能需要额外的 Dockerfile 自定义以避免将所有这些资产复制到您的 Dockerfile 中。通常,给定解决方案/项目的文件夹结构越复杂,由 Visual Studio 创建的 Dockerfile 就越有可能需要定制以适应项目的特定布局。

于 2021-11-17T00:43:37.567 回答