首页 > 解决方案 > 如何使用项目解决方案创建 docker 容器,其中 lib 项目位于比构建上下文高一级的位置

问题描述

我有一个 VS2017 (v5.18.0) 解决方案,其中包含一个 .NET Core 2.0 控制台应用程序“ReferenceGenerator”作为“启动”应用程序。该解决方案还包含两个 .Net Core lib 2.0 项目 FwCore 和 LibReferenceGenerator,它们是“本土”库。我添加了 docker 支持 (Linux),因此添加了创建 docker 应用程序所需的所有文件。我可以使用“docker for windows in Linux mode”在“docker-compose”模式下调试应用程序。并且该应用程序运行良好。如果我尝试构建发布版本,我会收到一个错误,即从非法路径发生 COPY。docker 文件如下所示:

FROM microsoft/dotnet:2.0-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.0-sdk AS build
WORKDIR /src
COPY ReferenceGenerator/ReferenceGenerator.csproj 
ReferenceGenerator/
COPY ../LibReferenceGenerator/LibReferenceGenerator.csproj ../LibReferenceGenerator/
COPY ../FwCore/FwCore/FwCore.csproj ../FwCore/FwCore/
RUN dotnet restore 
ReferenceGenerator/ReferenceGenerator.csproj
COPY . .
WORKDIR /src/ReferenceGenerator
RUN dotnet build ReferenceGenerator.csproj -c Release -o /app

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

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

内容下方的行:

COPY ../LibReferenceGenerator/LibReferenceGenerator.csproj ../LibReferenceGenerator/

导致错误:

Step 6/17 : COPY ../LibReferenceGenerator/LibReferenceGenerator.csproj ../LibReferenceGenerator/
1>Service 'referencegenerator' failed to build: COPY failed: Forbidden path outside the build context: ../LibReferenceGenerator/LibReferenceGenerator.csproj ()

我读过不允许相对路径,就这样吧。但是编译器的输出已经在ReferenceGenerator项目的bin目录下完成了。我已经尝试删除引用库的两个复制行,但随后构建抱怨在 dotnet 构建阶段缺少 lib 项目文件。

在我看来,解决方案中包含一些“homebuild”lib 项目似乎是一种非常常见的情况。我是 docker 容器的新手,我不知道如何解决这个问题,有人吗?

附加信息我的文件结构如下所示:

/Production/ReferenceGenerator/ReferenceGenerator.sln
/Production/ReferenceGenerator/ReferenceGenerator/ReferenceGenerator.csproj
/Production/LibReferenceGenerator/LibReferenceGenerator.csproj
/Production/FwCore/FwCore/FwCore.csproj
/Production/ReferenceGenerator/ReferenceGenerator/Dockerfile

请任何人。试图帮助我的人没有成功。我完全陷入了开发中......

标签: docker.net-corevisual-studio-2017docker-composesolution

解决方案


答案是,没有解决办法...

如果您需要库,则必须使用(私有)nuget 库来包含它们。这不是一个简洁的解决方案,因为在调试时,您没有可用的库源,但在构建上下文之外包含库是不行的,我学会了研究互联网......

同样在微服务环境中共享代码应该被最小化以避免团队破坏其他团队的代码。对于所有喜欢为这个问题提供解决方案的开发人员感到抱歉,除了使用 nuget 包的解决方法之外,没有!


推荐阅读