如何为 .NET 解决方案构建具有多个依赖项的 Docker 图像?

How to build a Docker image with multiple dependencies for .NET Solution?

它可能有点长,但我认为它很容易阅读...那么让我们开始吧...

我有一个使用 IoT Edge Tools 创建的物联网边缘解决方案。 它生成一个 Dockerfile,它适用于没有外部 DLL 作为依赖项的项目。

当我在此模块 (MyEdgeModule) 中添加多个 DLL(MySqlAdapterBaseAdapterHelper) 时出现问题,但在尝试时没有更改 Dockerfile构建图像,

它抛出一个错误:

Skipping project "/MySqlAdapter/MySqlAdapter.csproj" because it was not found. Skipping project "/MySqlAdapter/MySqlAdapter.csproj" because it was not found.

...

... : warning : The referenced project '../MySqlAdapter/MySqlAdapter.csproj' does not exist. [/app/MyEdgeModule.csproj]

这里是为 MyEdgeModule

生成的 Dockerfile
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.1-runtime-stretch-slim
WORKDIR /app
COPY --from=build-env /app/out ./

RUN useradd -ms /bin/bash moduleuser
USER moduleuser

ENTRYPOINT ["dotnet", "MyEdgeModule.dll"]

这些 class 库项目位于 Dockerfile 中的“../{projname}/*.csproj”。

所以我尝试将这些项目分别添加到 Dockerfile 中,看看是否可行,但没有成功。

我什至从 documentation 那里了解到:

The path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

据我所知,我的 context 是我试图从中返回的文件夹,以找到我的依赖项目,这似乎是不可能的。

我如何为这个项目创建一个图像,假设它有几十个外部 DLL 作为依赖项?

目前可行的方法是创建一个新的 .NET Core 项目并使用 Add Docker Support,每次生成它时,它都会使用参考资料中的所有依赖项刷新它的 Dockerfile。

为了工作,需要将 Dockerfile 移动到解决方案目录,而不是在生成它的项目文件夹中。

自动生成的 Dockerfile 在我的情况下不起作用。

COPY failed: stat /var/lib/docker/tmp/docker-builder471019165/src/server/MyWebService/MyWebService.csproj: no such file or directory