docker - Azure Pipelines从Container Registry构建Docker镜像

标签 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和相同的效果。我该如何工作?

最佳答案

这是我一直在使用的:

- 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 - Azure Pipelines从Container Registry构建Docker镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56024471/

相关文章:

bash - Dockerfiles中&&和 `set -ex`的区别

git - 通过 API 在 ECS 上挂载目录?

azure - 有条件地设置环境 Azure DevOps

azure-devops - Azure DevOps 将 Nuget 发布到托管源

azure-devops - 一次分组和触发多个 Azure Pipelines

docker - 管理 docker 容器安全补丁

docker - 如何在 kubernetes 中运行的 docker 中模拟 --device 选项

.net - 使用 nuget 包将配置添加到 appsettings.json

Azure cron 管道始终运行

azure-pipelines - 通过 webhook 触发 azure 管道?