Is it possible to build docker images with Docker compose task in Azure Devops pipelines and give it a specific name?
Because the names I get to the images look like $(Build.Repository.Name) only.
What I tried:
steps:
- task: DockerCompose#0
inputs:
containerregistrytype: 'Container Registry'
dockerRegistryEndpoint: 'Docker_Hub'
dockerComposeFile: '**/docker-compose.yml'
qualifyImageNames: false
action: 'Run a Docker Compose command'
dockerComposeCommand: 'build'
and without the qualifyImageNames too.
I tried this also but I got an image name of xxx_{some name from the docker-compose.yaml}:
- task: DockerCompose#0
inputs:
containerregistrytype: 'Container Registry'
dockerRegistryEndpoint: 'Docker_Hub'
dockerComposeFile: '**/docker-compose.yml'
projectName: 'xxx'
action: 'Build services'
includeLatestTag: true
but I want to name the image something like myreponame/somename:latest.
I have 2 images the docker-compose need to build.
Related
Trying to get my Azure DevOps CI/CD Pipeline to push a bunch of images to Azure Container Registry, although it continues to fail
The process 'C:\Windows\system32\docker.exe failed with exit code 1
DockerCompose
Pipeline result error
Error analysis
YAML
# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
branches:
include:
- Initial-K8-Setup
resources:
- repo: self
clean: true
# define variables
variables:
# Container registry service connection established during pipeline creation
azureSubscription: 'HAWeb2-CFS-NonProd'
# Container Registry Type
crType: 'Azure Container Registry'
# Container Name
AZURE_CONTAINER_REGISTRY: 'crschacfsnonprod' #.azurecr.io'
dockerComposeFileArgs: 'REGISTRY=crschacfsnonprod.azurecr.io/'
dockerComposeFile: '**/docker-compose.yml'
# Additional Docker File
addDockerComposeFiles: 'docker-compose.override.yml'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
dockerRegistryServiceConnection: '7739b21f-2572-4f36-ab8b-df15ab821756'
imageName: aquarium
imageTag: demo
# Agent VM image name
vmImageName: 'windows-2019'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
# Log into ACR Registry
- script: |
docker login crschacfsnonprod.azurecr.io/ -u $(acrUsername) -p $(acrPassword2)
env:
acrLoginServer: crschacfsnonprod.azurecr.io
acrUsername: $(acrUsername)
acrPassword: $(acrPassword2)
displayName: 'Docker login to ACR'
# Build Images
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: 'HAWeb2-CFS-NonProd'
azureContainerRegistry: '{"loginServer":"crschacfsnonprod.azurecr.io", "id" : "/subscriptions/5e2acfcb-7aeb-443d-9ea4-bc7553a51276/resourceGroups/rg-ha-cfs-nonprod-001/providers/Microsoft.ContainerRegistry/registries/crschacfsnonprod"}'
dockerComposeFile: '**/docker-compose.yml'
additionalDockerComposeFiles: 'docker-compose.override.yml'
dockerComposeFileArgs: 'REGISTRY=crschacfsnonprod.azurecr.io/'
action: 'Build services'
additionalImageTags: '$(Build.BuildNumber)-$(Build.SourceBranchName)'
includeLatestTag: true
# Push Images
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: 'HAWeb2-CFS-NonProd'
azureContainerRegistry: '{"loginServer":"crschacfsnonprod.azurecr.io", "id" : "/subscriptions/5e2acfcb-7aeb-443d-9ea4-bc7553a51276/resourceGroups/rg-ha-cfs-nonprod-001/providers/Microsoft.ContainerRegistry/registries/crschacfsnonprod"}'
dockerComposeFile: '**/docker-compose.yml'
additionalDockerComposeFiles: 'docker-compose.override.yml'
dockerComposeFileArgs: 'REGISTRY=crschacfsnonprod.azurecr.io/'
action: 'Push services'
additionalImageTags: '$(Build.BuildNumber)-$(Build.SourceBranchName)'
includeLatestTag: true
# Copy Files
- task: CopyFiles#2
inputs:
SourceFolder: 'k8s'
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)/k8s'
# Publish Artifact
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifact: 'Sitecore-Aquarium-XM1'
publishLocation: 'pipeline'
Configured a Docker Registry Service Connection
Azure Container Registry
SKU: Premium
Admin User: Enabled
Identity
System assigned: Off
User Assigned: None
Networking:
Public network access: All networks
Now I am unsure how the above Pipeline actually ads up... When looking into Repositories within the ACR, something have actually been pushed, although why does the Pipeline fail?
What am I doing wrong?
Can anyone clarify what I might have to do, to resolve this?
I am trying to build and push an image to Azure Container Registry using Azure DevOps pipeline, but I am getting the following error:
"base name (${BUILD_IMAGE}) should not be blank"
when trying to build the image.
I have checked my pipeline configuration file and I couldn't find the variable BUILD_IMAGE defined. From the error message, it seems that the variable is being used in the FROM statement in the Dockerfile but I don't know where it's supposed to be defined.
I have checked the pipeline configuration file, the script that triggers the pipeline, and the command-line but I couldn't find where the variable is defined.
I have also checked if the image aquariumcms with tag $(Build.BuildId) or 510914 exists in the registry rgaquariumtestimagewesteu.azurecr.io and if it's accessible from the pipeline but I couldn't find any issues.
My pipeline is using the Docker#2 and DockerCompose#0 tasks, and the container registry is rgaquariumtestimagewesteu.azurecr.io.
How can I add the variable BUILD_IMAGE so it's no longer blank and the pipeline can build and push the image to the container registry?
The pipeline
# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- Initial-K8-Setup
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: '7739b21f-2572-4f36-ab8b-df15ab821756'
imageRepository: 'aquariumcms'
containerRegistry: 'rgAquariumTestImageWestEU.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker#2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: 'HAWeb2-CFS-NonProd'
azureContainerRegistry: '{"loginServer":"rgAquariumTestImageWestEU.azurecr.io", "id" : "/subscriptions/5e2acfcb-7aeb-443d-9ea4-bc7553a51276/resourceGroups/rg-haweb-cfs-nonprod-web2-001/providers/Microsoft.ContainerRegistry/registries/rgAquariumTestImageWestEU"}'
dockerComposeFile: '**/docker-compose.yml'
additionalDockerComposeFiles: 'docker-compose.override.yml'
dockerComposeFileArgs: 'REGISTRY=rgAquariumTestImageWestEU.azurecr.io/'
action: 'Build services'
additionalImageTags: '$(Build.BuildNumber)-$(Build.SourceBranchName)'
includeLatestTag: true
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: 'HAWeb2-CFS-NonProd'
azureContainerRegistry: '{"loginServer":"rgAquariumTestImageWestEU.azurecr.io", "id" : "/subscriptions/5e2acfcb-7aeb-443d-9ea4-bc7553a51276/resourceGroups rg-haweb-cfs-nonprod-web2-001/providers/Microsoft.ContainerRegistry/registries/rgAquariumTestImageWestEU"}'
dockerComposeFile: '**/docker-compose.yml'
additionalDockerComposeFiles: 'docker-compose.override.yml'
dockerComposeFileArgs: 'REGISTRY=rgAquariumTestImageWestEU.azurecr.io/'
action: 'Push services'
additionalImageTags: '$(Build.BuildNumber)-$(Build.SourceBranchName)'
includeLatestTag: true
The expectation was that the pipeline would build and push the image to the Azure Container Registry specified in the pipeline configuration file. However, the pipeline is failing with the error message "base name (${BUILD_IMAGE}) should not be blank" when trying to build the image, indicating that the BUILD_IMAGE variable is empty or not defined.
Although I receive the following error log
Starting: Build and push an image to container registry
==============================================================================
Task : Docker
Description : Build or push Docker images, login or logout, start or stop containers, or run a Docker command
Version : 2.214.0
Author : Microsoft Corporation
Help : https://aka.ms/azpipes-docker-tsg
==============================================================================
/usr/bin/docker build -f /home/vsts/work/1/s/Dockerfile --label com.azure.dev.image.system.teamfoundationcollectionuri=https://dev.azure.com/dgsit/ --label com.azure.dev.image.system.teamproject=Sitecore --label com.azure.dev.image.build.repository.name=aquarium-cms --label com.azure.dev.image.build.sourceversion=dad7e8024ac8bba9b7b58bce934cdc96729da5cb --label com.azure.dev.image.build.repository.uri=https://dgsit#dev.azure.com/dgsit/Sitecore/_git/aquarium-cms --label com.azure.dev.image.build.sourcebranchname=Initial-K8-Setup --label com.azure.dev.image.build.definitionname=aquarium-cms (1) --label com.azure.dev.image.build.buildnumber=20230118.7 --label com.azure.dev.image.build.builduri=vstfs:///Build/Build/511111 -t ***/aquariumcms:511111 /home/vsts/work/1/s
Sending build context to Docker daemon 15.5MB
Step 1/29 : ARG BASE_IMAGE
Step 2/29 : ARG BUILD_IMAGE
Step 3/29 : FROM ${BUILD_IMAGE} AS prep
base name (${BUILD_IMAGE}) should not be blank
##[error]base name (${BUILD_IMAGE}) should not be blank
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Build and push an image to container registry
this is working for me, it overwrites the "latest" version in the repo as well as creates a new independent version based on the BuildID.
Under build added below tags.
tags: |
latest
$(Build.BuildId)
Here is my sample azure-pipeline.yml
# Docker
trigger:
- main
resources:
- repo: self
variables:
dockerRegistryServiceConnection: '...'
imageRepository: 'mycontainer'
containerRegistry: 'azuk.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/dockerfile'
tag: '$(Build.BuildId)'
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker#2
displayName: 'Build and Push $(container.registry)'
inputs:
containerRegistry: '$(container.registry)'
repository: '$(container.repository)'
Dockerfile: '**/Dockerfile'
command: 'buildAndPush'
tags: |
latest
$(Build.BuildId)
refer to this github issue [8378](Docker push no longer pushes *:latest tag with includeLatestTag: true · Issue #8378 · microsoft/azure-pipelines-tasks (github.com)) for more information regardign similar issue
I have pushed a new image in the azure container registry
But i am unable to find the image in azure container registry
Please find the deplyment yaml file
name: Dotnet Code Build and Push
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: ubuntu-18.04
steps:
- checkout: self
- task: Docker#0
displayName: Build an image
inputs:
azureSubscription: 'sc-***dp'
azureContainerRegistry:
loginServer: *****aksacr.azurecr.io
id: "/subscriptions/4f76bb2f-c521-45d1-b311-b87bf**747ff/resourceGroups/eus-***dp-rg/providers/Microsoft.ContainerRegistry/registries/*****aksacr"
imageName: ***dpacr.azurecr.io/ims-***dp/$(Build.Repository.Name):$(Build.BuildId)
- task: Docker#2
displayName: Login to ACR
inputs:
command: login
containerRegistry:'sc-***dp'
- task: Docker#2
displayName: Push an image
inputs:
azureSubscription: 'sc-***dp'
repository: ims-***dp
azureContainerRegistry: '{"loginServer":"*****aksacr.azurecr.io", "id" : "/subscriptions/4f76bb2f-c521-45d1-b311-b87bf**747ff/resourceGroups/eus-***dp-rg/providers/Microsoft.ContainerRegistry/registries/*****aksacr"}'
action: Push an image
I would recommend to create a new service connection with the container registry if one does not exist.
and select Azure Container Registry.
Then you can push in your registry using the below task. Keep in mind that ${{container}} should have a notation as xxx.azurecr.io/imagename:tag
- task: Docker#2
displayName: pushing image ${{container}}
inputs:
containerRegistry: 'serviceConnectionName'
repository: '${{container}}'
command: 'push'
tags: |
mytag
Try to push the code in azure devops pipeline so you can directly push image in azure container registry
Here we can docker task it can build up and push the image with help of setting and it take the image from repository and then it can push into the azure container registry
task: Docker#2
displayName: Push an image
inputs:
command: buildAndpush
repository: $(imageRepository)
dockerfile: $(dockerfilepath)
ContainerRegistry: $(dockerRepositoryserviceconnectio)
tags:
$(tag)
Save and run.
Note : After running you can't see Repository please refresh again because in azure pipeline the job may be still running once the job is completed & successfully run and refresh you can able see image is pushed
Please refer this document by rajaniesh kaushikk has given detail information
Please check the repository and containerRegistry value in your Docker#2 task, make sure they points to target resource. My script for your reference below. Please note the parameters in Docker#2 task, no azureContainerRegistry and command needed.
variables:
dockerRegistryServiceConnection: acrconn1
imageRepository: 'acrlearn'
containerRegistry: 'test.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'windows-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker#2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
Compose Push Service throws the below error in my pipeline:
##[error]Unhandled: Failed find:
EACCES: permission denied, scandir '/home/vsts/work/1/s/data'
Pipeline YAML- I am using Docker Compose commands, the Build is working fine, its the push to ACR fails
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
pool:
vmImage: ubuntu-latest
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'GUID'
imageRepository: 'strapiregimg'
containerRegistry: 'strapireg.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
enter code here
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: ''
azureContainerRegistry: '{"loginServer":"strapireg.azurecr.io", "id" : ""}'
dockerComposeFile: '**/docker-compose.yml'
action: 'Build services'
includeSourceTags: true
includeLatestTag: true
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: ''
azureContainerRegistry: '{"loginServer":"strapireg.azurecr.io", "id" : "'
dockerComposeFile: '**/docker-compose.yml'
action: 'Run services'
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: ''
azureContainerRegistry: '{"loginServer":"strapireg.azurecr.io", "id" : ""}'
dockerComposeFile: '**/docker-compose.yml'
action: 'Push services'
Error is on Azure Pipeline only, locally it works well
Posting the Answer based on PGautami comment and supporting the statement given by jessehouwing to help the other community members with related discussions.
When you are using Docker compose commands to Push services through ACR if you run docker run command prior to docker push it will help you in preventing from file locks so that the Push command works.
I am unable to follow the documentation for logging into my container registry in Azure using the task "DockerCompose#0".
The documentation suggests to use:
variables:
azureContainerRegistry: Contoso.azurecr.io
azureSubscriptionEndpoint: Contoso
steps:
- task: DockerCompose#0
displayName: Container registry login
inputs:
containerregistrytype: Azure Container Registry
azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
azureContainerRegistry: $(azureContainerRegistry)
Where azureSubscriptionEndpoint is the name of the Service Connection for ARM.
I used the following pipeline:
pool:
name: Hosted Ubuntu 1604
variables:
azureContainerRegistry: <ACRName>.azurecr.io
azureSubscriptionEndpoint: <SubscriptionName> (<SubscriptionID>)
steps:
- task: DockerCompose#0
displayName: Container registry login
inputs:
containerregistrytype: Azure Container Registry
azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
azureContainerRegistry: $(azureContainerRegistry)
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
azureContainerRegistry: '{"loginServer":"<ACRName>.azurecr.io", "id" : "/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>/providers/Microsoft.ContainerRegistry/registries/<ACRName>"}'
dockerComposeFile: '<Path>/docker-compose.yml'
action: 'Build services'
- task: DockerCompose#0
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
azureContainerRegistry: '{"loginServer":"<ACRName>.azurecr.io", "id" : "/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup>/providers/Microsoft.ContainerRegistry/registries/<ACRName>"}'
dockerComposeFile: '<Path>/docker-compose.yml'
action: 'Push services'
- task: CopyFiles#2
displayName: "Copy Files to: $(Build.ArtifactStagingDirectory)"
inputs:
SourceFolder: <Path>
Contents: '**\docker-compose.yml'
TargetFolder: "$(Build.ArtifactStagingDirectory)"
- task: PublishBuildArtifacts#1
displayName: "Publish Artifact: drop"
However, I am receiving following error in my pipeline:
##[error]Input required: dockerComposeCommand
for the the first task (Container registry login).
Where am I going wrong?
I guess you don't need the login step from your build, docker tasks handle Auth using azure subscription endpoint if it is pre configured, if not - give your service principal permissions to acrpush.
You misinterpret the documentation. You don't need a separate login step, you have to specify the login information on each DockerCompose Task you are using.