Azure Pipelines从Container Registry构建Docker映像

时间:2019-05-07 14:06:14

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

我在Azure容器注册表中有一个Dockerfile的基本映像。当我在本地运行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和相同的效果。我该如何使用它?

1 个答案:

答案 0 :(得分:0)

这是我一直在使用的:

- 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会为您完成