Azure Devops Build Pipeline - 使用引用项目构建 docker 时出现问题

标签 azure docker azure-devops azure-pipelines azure-pipelines-build-task

我对 docker/azure DevOps 构建管道完全陌生。但我不得不承认,这是一条痛苦的路。这应该很容易,但事实并非如此:)

我想要实现的是仅构建一个项目,该项目在解决方案中具有一个引用的项目。但我不想构建整个解决方案(因为我有 Xamarin 项目,需要更长的时间来构建)至于 SLN - 我想答案会更容易找到。

当只有一个项目而没有引用其他项目时,我确实让这一管道正常工作。但现在,正如我引用的那样,它崩溃了。

仅供记录,我使用构建管道来创建 Docker 镜像并将其部署到 Azure 容器注册表。但我认为这并不重要,因为它构建失败。

我的代码结构

  • 解决方案

    • [项目] BuildChat <-- SignalR 引用的项目
    • [项目] Xamarin.IOS
    • [项目] Xamarin.Android
    • [项目] SignalRChat
      • [文件]Docker

我的初始文件是通过 VS 上的 Docker 支持插件创建的(此处建议:asp.net core 2.0 - multiple projects solution docker file)

当然,当我第一次使用它时,它没有引用的项目。所以我想,好吧,也许我会删除 docker 文件并再次使用 Add-->Docker 支持。它确实更改了 dockerfile 以包含引用的项目(称为 BuildChat)

因此,更改后我的 docker 文件如下所示:

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

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["SignalRChat/SignalRChat.csproj", "SignalRChat/"]
COPY ["BuildChat/BuildChat.csproj", "BuildChat/"]
RUN dotnet restore "SignalRChat/SignalRChat.csproj"
COPY . .
WORKDIR "/src/SignalRChat"
RUN dotnet build "SignalRChat.csproj" -c Release -o /app/build

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

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

但是它不起作用。可能是因为 docker 无法脱离上下文。但话又说回来,docker 支持不知道这一点?:D

我认为一个选项是将其移动到 SLN 级别,然后使用 docker build 和 -f 来指定文件(我将此视为建议),但我不知道如何将其包含在 azure 构建中管道。

也许还有一些关于如何实现这一点的其他想法(在这个 docker“hello world”程序中,结果非常复杂:D)

我当前的构建管道如下:

# Docker
# Build a Docker image 
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  dockerRegistryServiceConnection: 'MyProductDockerACR'
  imageRepository: 'mobile/signalr'
  containerRegistry: 'myAcrName.azurecr.io'
  dockerfilePath: '**/Dockerfile'
  tag: '$(Build.BuildNumber)'
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push image to container registry
      inputs:
        containerRegistry: $(dockerRegistryServiceConnection)
        repository:  $(imageRepository)
        command: 'buildAndPush'
        Dockerfile: $(dockerfilePath)
        tags: |
          $(tag)

构建管道产生的错误是(剪切以呈现有趣的部分:)):

Starting: Build and push image to container registry
==============================================================================
Task         : Docker
Description  : Build or push Docker images, login or logout, or run a Docker command
Version      : 2.166.1
Author       : Microsoft Corporation
Help         : https://aka.ms/azpipes-docker-tsg
==============================================================================
/usr/bin/docker build -f /home/vsts/work/1/s/SignalRChat/Dockerfile --label com.azure.dev.image.system.teamfoundationcollectionuri=https://dev.azure.com/myAzureDevops/ --label com.azure.dev.image.system.teamproject=Mobile --label com.azure.dev.image.build.repository.name=Mobile --label com.azure.dev.image.build.sourceversion=e1fcddf1507478434251b2c12a3f999ef23f7b70 --label com.azure.dev.image.build.repository.uri=https://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca1b58db6b9bea988a9baa3bcbf8ca8a9bae2adb6b9bea9e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>/myAzureDevops/Mobile/_git/Mobile --label com.azure.dev.image.build.sourcebranchname=master --label com.azure.dev.image.build.definitionname=Mobile --label com.azure.dev.image.build.buildnumber=20200423.7 --label com.azure.dev.image.build.builduri=vstfs:///Build/Build/26 -t ***/mobile/signalr:20200423.7 /home/vsts/work/1/s/SignalRChat
Sending build context to Docker daemon  2.509MB

[...]

Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-buster
 ---> 4aa6a74611ff
Step 6/27 : WORKDIR /src
 ---> Running in abca9a0a7296
Removing intermediate container abca9a0a7296
 ---> 15699d4bddc2
Step 7/27 : COPY ["SignalRChat/SignalRChat.csproj", "SignalRChat/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder654251088/SignalRChat/SignalRChat.csproj: no such file or directory
##[error]COPY failed: stat /var/lib/docker/tmp/docker-builder654251088/SignalRChat/SignalRChat.csproj: no such file or directory
##[error]The process '/usr/bin/docker' failed with exit code 1

[更新 14:00]

将 docker 文件移动到解决方案级别可以解决该问题。但问题仍然存在。如果我想将另一个项目 Docker 化怎么办?那么如何进行?

最佳答案

我认为您的顺序可能有误

COPY ["SignalRChat/SignalRChat.csproj", "SignalRChat/"]
COPY ["BuildChat/BuildChat.csproj", "BuildChat/"]

例如我有两个项目

  • SomeLib.csproj - 该库由 WeatherService.csproj 引用
  • WeatherService.csproj

对于此订单:

COPY ["WeatherService/WeatherService.csproj", "WeatherService/"]
COPY ["SomeLib/SomeLib.csproj", "SomeLib/"]

我得到了这个:

enter image description here

但是对于这个订单它是有效的

COPY ["SomeLib/SomeLib.csproj", "SomeLib/"]
COPY ["WeatherService/WeatherService.csproj", "WeatherService/"]

enter image description here

您可以尝试更改COPY顺序吗:

COPY ["BuildChat/BuildChat.csproj", "BuildChat/"]
COPY ["SignalRChat/SignalRChat.csproj", "SignalRChat/"]

编辑:

我直接在 Azure DevOps 上测试了上面编写的内容,一切顺利。

这是我的DockerFile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["SomeLib/SomeLib.csproj", "SomeLib/"]
COPY ["WeatherService/WeatherService.csproj", "WeatherService/"]
RUN dotnet restore "WeatherService/WeatherService.csproj"
COPY . .
WORKDIR "/src/WeatherService"
RUN dotnet build "WeatherService.csproj" -c Release -o /app/build

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

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

这是项目结构:

Project structure

和 YAML 文件:

- task: Docker@2
  displayName: Build and Push
  inputs:
    repository: $(imageName)
    command: build
    Dockerfile: docker-multiple-apps/Dockerfile
    tags: |
      build-on-agent

所有你能找到的here .

这是来自 Azure DevOps 的日志

log from Azure DevOps

您能将其与您的方法进行比较吗?抱歉,我无法直接指出根本原因,但它适用于我的引用项目。

编辑2:

您的解决方案名称是什么?是 SignalRChat 吗?这是你的根文件夹吗?

/home/vsts/work/1/s/SignalRChat/Dockerfile

也许你的 Dockerfile 路径错误?

关于Azure Devops Build Pipeline - 使用引用项目构建 docker 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61384644/

相关文章:

azure - 我可以从可用性集中删除 Azure VM

linux - 为什么要在 docker 文件中连接安装参数?

angular - 在 Azure Web 应用程序服务中部署 Angular 通用应用程序 - 基于 Linux

azure-devops - 如何在 Team Services 中替换 JSON 文件的变量?

azure - 如何使用 AzureADGraph 或 Microsoft Graph 在 AZURE AD B2C 中为用户生成访问 token ?

azure - 如何创建具有 vnet 集成的 azure 标准逻辑应用程序,以启用不同 azure 订阅中的存储帐户之间的访问?

c# - 自定义 Log4Net 附加程序未正确创建表或存储条目

linux - Docker macvlan 网络,无法访问互联网

linux - curl : (3) URL using bad/illegal format

azure - 使用 Azure DevOps 中的 Azure 文件复制任务将数据从存储帐户复制到存储帐户