Visual Studio docker-撰写构建上下文

Visual Studio docker-compose build context

将容器协调器支持(docker-compose)添加到依赖于某些项目库的 .NET Core Web API 项目时,会创建以下文件夹结构

├── Solution
│   ├── API.Project
|   |   ├──  API.Project.csproj
|   |   ├──  Dockfile
|   |   
|   ├── Library.project
|   |   ├──  Library.project.csproj
|   |   
|   ├── docker-compose.yaml   

如您所见,库项目在 Dockerfile 上下文之外。如果我使用 docker/build-push-action@v2 (https://github.com/marketplace/actions/build-and-push-docker-images) 在我的 Github Action 管道中构建图像,它找不到库项目。如果我将 Dockerfile 移动到解决方案文件夹并构建图像和 运行 一个容器,visual studio 调试器将不会附加,但容器会 运行。但是,当我向容器发出 http 请求时,空指针异常会记录在容器日志中(也在来自 github 动作图像的容器中)。如何构建具有像本示例这样的文件夹结构的 docker 图像?我也希望将 Dockerfile 保存在 API.project 文件夹中。

使用 docker/build-push-action@v2,您可以指定 docker 文件的上下文和位置,如下所示:

name: Build and push
    uses: docker/build-push-action@v2
    with:
      context: .
      file: API.Project/Dockerfile
      push: true
      tags: user/app:latest

这允许您将文件包含在 Dockerfile 的父文件夹中。

我在将 Dockerfile 移动到父文件夹时收到的空指针异常与对 System.Security.Cryptography 的依赖有关,但我不必解决它,因为指定 docker构建上下文并将 docker 文件保存在 API.project 文件夹中解决了我的问题