【问题标题】:Azure Pipelines build Docker Image from Container RegistryAzure Pipelines 从 Container Registry 构建 Docker 映像
【发布时间】:2019-09-25 05:26:50
【问题描述】:

我的 Dockerfile 有一个基本映像,位于 Azure 容器注册表中。当我在本地运行 Docker 时,图像会构建,但是当我尝试在 Azure Pipelines 中运行它时,它在 Get 上失败:“未授权:需要身份验证”。但是,我在我的 DevOps 项目上创建了一个服务连接(并使其可用于所有管道)并按照the docs 使用它。

这是我的 dockerfile:

FROM myregistry.azurecr.io/bases/netcorenodebase:v1.0 AS base
WORKDIR /app

EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app

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

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

管道 YAML:

pool:
  name: Hosted Ubuntu 1604

variables:
  buildConfiguration: 'Release'

steps:
- task: Docker@2
  displayName: Login to ACR
  inputs:
    command: login
    containerRegistry: $(dockerServiceConnectionName)
- task: Docker@2
  displayName: Build
  inputs:
    command: build
    containerRegistry: $(dockerServiceConnectionName)
    repository: myrepo/myimage
    tags: |
      $(Build.BuildId)
      latest
- task: Docker@2
  displayName: Push
  inputs:
    command: push
    containerRegistry: $(dockerServiceConnectionName)
    repository: myrepo/myimage
    tags: |
      $(Build.BuildId)
      latest
- task: Docker@2
  displayName: Logout of ACR
  inputs:
    command: logout
    containerRegistry: $(dockerServiceConnectionName)

dockerServiceConnectionName 变量设置为服务连接的名称,并在登录阶段成功。但似乎上下文没有传递给 Docker 守护进程,因此它无法访问 ACR。我也尝试过 buildAndPush 并且效果相同。我怎样才能让它工作?

【问题讨论】:

    标签: docker azure-devops azure-pipelines azure-container-registry


    【解决方案1】:

    这是我一直在使用的:

    - task: Docker@1
      inputs:
        containerregistrytype: 'Container Registry'
        dockerRegistryEndpoint: registryName
        imageName: imageName
        includeLatestTag: true
        dockerFile: path_to_file
    
    - task: Docker@1
      inputs:
        containerregistrytype: 'Container Registry'
        dockerRegistryEndpoint: registryName
        imageName: imageName
        command: push
    

    您不必登录\注销,docker step 会为您完成此操作

    【讨论】:

    • docker如何知道它需要向哪个用户进行身份验证?因为我们没有在任何地方传递任何凭据?
    • 你可以指定任务中使用的服务连接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    相关资源
    最近更新 更多