如何构建docker映像并推送到Azure容器注册表

问题描述 投票:0回答:1

我正在使用Azure Pipelines,并已成功构建了一个管道,以构建docker映像并将其部署到Azure应用服务。

现在,我试图弄清楚如何在管道中构建docker映像,然后将其发布到工件暂存目录,以便将其推送到Azure容器注册表。我在Google各处四处寻找,看看运气如何,这将是一个三部分的问题。

我成功地在管道中构建了一个dockerfile,但我无法弄清楚如何删除一个临时文件(保持拒绝访问)

Removing intermediate container 543fb86650c4
 ---> 3ccb8c3a555e
Step 10/28 : RUN del -f "./NuGet.Config"
 ---> Running in c2ba7a3266a4
C:\src\NuGet.Config
Access is denied.

映像构建从那里继续并成功,但是我试图发布工件,我想获取构建的映像并将其传递到发布管道以推送到Azure容器注册表。发布工件失败,因为它找不到图像,我真的不确定如何找到它?

##[warning]Directory 'd:\a\1\a' is empty. Nothing will be added to build artifact 'Email'.

也许我要解决所有这些错误?如果是这样,请提出一个替代方案,只记得我想将docker映像推送到azure容器注册表而不是azure应用程序服务。

问题1:如何在管道任务中删除容器内的文件?

问题2:如何将Docker映像而不是Azure应用程序服务推送到Azure容器注册表?

我正在使用UI来配置我的管道,并且没有共享的完整yaml文件,但这是view选项中的yaml:

pool:
  name: Azure Pipelines
variables:
  Parameters.ArtifactName: 'Email'

steps:
- task: Docker@2
  displayName: 'Build an image'
  inputs:
    containerRegistry: 'Azure Container'
    repository: email
    command: build
    Dockerfile: Services/Email/Email/Dockerfile
    buildContext: Services/Email
    tags: latest
    arguments: '-o $(build.artifactstagingdirectory)'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
    ArtifactName: '$(Parameters.ArtifactName)'

问题3:一旦我将文件发布到工件暂存目录,我将如何访问该文件以将图像推送到容器注册表?

docker azure-devops azure-pipelines azure-pipelines-build-task azure-container-registry
1个回答
0
投票

如何删除管道任务中的文件?

您可以使用Delete Files任务从代理工作目录中删除文件或文件夹。对于下面的示例。

 - task: DeleteFiles@1
   displayName: 'Delete files'
   inputs:
     SourceFolder: '$(Build.SourcesDirectory)'
     Contents: NuGet.Config

dockerfile中的命令在容器中执行,delete files命令无法删除本地驱动器上的文件。要删除容器中的文件,可以检查this thread中讨论的方法。

2,要构建Docker映像并推送到Azure容器注册表

无需使用PublishBuildArtifacts任务。

您可以使用单个docker task运行buildandpush命令来构建映像并将其推送到Azure容器注册表。enter image description here

或者您可以将buildandpush命令分为docker build任务和docker push任务。对于以下示例:enter image description here

- task: Docker@2
  displayName: Build
  inputs:
    command: build
    repository: myrepo/alpinelevi
    dockerfile: dockerfile
    containerRegistry: myacr
    tags: |
      test1

- task: Docker@2
  displayName: Push
  inputs:
    command: push
    repository: myrepo/alpinelevi
    containerRegistry: myacr
    tags: |
      test1

希望以上帮助!

© www.soinside.com 2019 - 2024. All rights reserved.