'Build and push a docker image with build arguments from DevOps to ACR
I'm building a docker image in an Azure DevOps pipeline, then I want to push it to the Azure Container registry. The registry is already created and I've configured DevOps to use it, also the buildAndPush
Docker task works.
However, since the Docker build can be parametrized, I want to pass arguments to it and I can't do it using the buildAndPush
Docker task. So I've decided to do it in two steps, one for building and the other one for pushing. This way:
# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger: none
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'aaaa-aaaa-aaaa-aaaa-aaaaa'
imageRepository: 'ubuntu-qt'
containerRegistry: 'myregistry.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/docker-qt-build/Dockerfile'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: BuildPush
displayName: Build and push
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build ubuntu with qt 5.12.4 installed
inputs:
command: build
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
tags: 5.12.4
arguments: '--build-arg ubuntu_version=18.04 --build-arg qt_version_major=5.12 --build-arg qt_version_full=5.12.4'
- task: Docker@2
displayName: Push ubuntu with qt 5.12.4 installed
inputs:
command: push
repository: $(imageRepository)
containerRegistry: $(dockerRegistryServiceConnection)
tags: myregistry.azurecr.io/ubuntu-qt:5.12.4
Even if the build task works well, it doesn't seem to be able to find the image for pushing to the registry:
Starting: Push ubuntu with qt 5.12.4 installed
==============================================================================
Task : Docker
Description : Build or push Docker images, login or logout, or run a Docker command
Version : 2.170.2
Author : Microsoft Corporation
Help : https://aka.ms/azpipes-docker-tsg
==============================================================================
/usr/bin/docker images
/usr/bin/docker push ***/ubuntu-qt:***/ubuntu-qt:5.12.4
invalid reference format
##[error]invalid reference format
##[error]The process '/usr/bin/docker' failed with exit code 1
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-qt 5.12.4 e5aeb1027823 2 seconds ago 6.64GB
ubuntu 18.04 2eb2d388e1a2 12 days ago 64.2MB
node 10 5010eb9431a5 2 weeks ago 911MB
node 12 dfbb88cfffc8 2 weeks ago 918MB
buildpack-deps stretch 42d486287f16 2 weeks ago 835MB
debian 9 5df937d2ac6c 2 weeks ago 101MB
debian 8 72f79d3cb645 2 weeks ago 129MB
node 10-alpine 8e473595b853 2 weeks ago 83.5MB
node 12-alpine 057fa4cc38c2 5 weeks ago 89.3MB
jekyll/builder latest 12489946feab 7 weeks ago 674MB
alpine 3.9 78a2ce922f86 3 months ago 5.55MB
alpine 3.10 be4e4bea2c2e 3 months ago 5.58MB
alpine 3.8 c8bccc0af957 6 months ago 4.41MB
ubuntu 14.04 6e4f1fe62ff1 7 months ago 197MB
alpine 3.7 6d1ef012b567 17 months ago 4.21MB
mcr.microsoft.com/azure-pipelines/node8-typescript latest 9a948d360778 22 months ago 595MB
Finishing: Push ubuntu with qt 5.12.4 installed
Solution 1:[1]
Specify containerRegistry
in the build step as well and use 5.12.4
for tags
in the push step (instead of myregistry.azurecr.io/ubuntu-qt:5.12.4
).
Like this:
steps:
- task: Docker@2
displayName: Build ubuntu with qt 5.12.4 installed
inputs:
command: build
repository: $(imageRepository)
containerRegistry: $(dockerRegistryServiceConnection)
dockerfile: $(dockerfilePath)
tags: 5.12.4
arguments: '--build-arg ubuntu_version=18.04 --build-arg qt_version_major=5.12 --build-arg qt_version_full=5.12.4'
- task: Docker@2
displayName: Push ubuntu with qt 5.12.4 installed
inputs:
command: push
repository: $(imageRepository)
containerRegistry: $(dockerRegistryServiceConnection)
tags: 5.12.4
Solution 2:[2]
A question was asked about how to push to multiple registries with a single build. In order to do this you need to alias (via docker tag) the local image for each repository.
steps:
- task: Docker@2
displayName: Build
inputs:
command: build
repository: myimage
dockerfile: myimage.dockerfile
# note no containerRegistry here
buildContext: '$(Build.SourcesDirectory)'
tags: latest
- task: CmdLine@2
displayName: Alias registry1.io
inputs:
script: docker tag myimage:latest registry1.io/myimage:latest
- task: Docker@2
displayName: Push registry1.io
inputs:
command: push
repository: myimage
containerRegistry: registry1.io
tags: latest
- task: CmdLine@2
displayName: Alias registry2.io
inputs:
script: docker tag myimage:latest registry2.io/myimage:latest
- task: Docker@2
displayName: Push registry2.io
inputs:
command: push
repository: myimage
containerRegistry: registry2.io
tags: latest
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | h3yduck |
Solution 2 | David Brink |