Search code examples
azure-devopsazure-pipelinesazure-pipelines-yamlazure-pipelines-tasks

I am having issues with docker compose task in azure pipeline


This is my dev.yml which is an azure pipeline for dev environment:

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- dev

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: '53d49b4c-9b33-4d35-b5f7-44d8554a609a'
  imageRepository: 'apidevloyaledge'
  containerRegistry: 'devsahasholdingcr.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'

pool:
  name: builders

stages:
- stage: Build
  displayName: Build the image
  jobs:
  - job: Build
    displayName: Build
    steps:
    
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: 'docker-compose.yml'
        artifact: 'docker-compose'
        publishLocation: 'pipeline'

    

    - task: Docker@2
      displayName: Build
      inputs:
        containerRegistry: '$(dockerRegistryServiceConnection)'
        repository: '$(imageRepository)'
        command: 'build'
        Dockerfile: 'Dockerfile'
        tags: '$(tag)'

- stage: Push
  displayName: Push the image
  jobs:
  - job: Push
    displayName: Push
    steps:
    - task: Docker@2
      displayName: Push an image to container registry
      inputs:
        containerRegistry: '$(dockerRegistryServiceConnection)'
        repository: '$(imageRepository)'
        command: 'push'
        tags: '$(tag)'

- stage: DeployToServer
  displayName: Deploy
  jobs:
  - deployment: dockerDeployment
    displayName: Deployment to dev environment
    environment:
      name: dev
    pool:
      name: dev
      demands: Agent.Name -equals dev-1

    strategy:
      runOnce:
        deploy:
          steps:
    
            - task: DownloadPipelineArtifact@2
              inputs:
                buildType: 'current'
                artifactName: 'docker-compose'
                targetPath: '.'
                
            - task: DockerCompose@0
              inputs:
                containerregistrytype: 'Azure Container Registry'
                azureSubscription: 'Microsoft Azure Sponsorship(e6037dae-cb96-472a-a3a9-809f0b83ba39)'
                azureContainerRegistry: '{"loginServer":"devsahasholdingcr.azurecr.io", "id" : "/subscriptions/e6037dae-cb96-472a-a3a9-809f0b83ba39/resourceGroups/dev/providers/Microsoft.ContainerRegistry/registries/devsahasholdingcr"}'
                dockerComposeFile: 'docker-compose.yml'
                dockerComposeFileArgs: 'tag = $(tag)'
                action: 'Run a Docker Compose command'
                dockerComposeCommand: 'up'
                arguments: '-d api-dev-loyaledge'

This is my docker-compose file:

version: "3.9"

services:
  api-dev-loyaledge:
    image: devsahasholdingcr.azurecr.io/apidevloyaledge:${tag}  
    container_name: api-dev-loyaledge  
    network_mode: "host"  
    restart: unless-stopped 
    environment: 
      - ASPNETCORE_ENVIRONMENT=Development
      
  api-testing-loyaledge:
    image: testingsahasholdingcr.azurecr.io/apitestingloyaledge:${tag}  
    container_name: api-testing-loyaledge
    network_mode: "host"  
    restart: unless-stopped 
    environment:
      - ASPNETCORE_ENVIRONMENT=testing

While my pipeline reaches to Docker compose task the following error occurs: Error in pipeline

I tried updating docker-compose file version to 3.9 and also I have updated the docker compose version in my deployment server to 2.12.2. Previously the same pipeline was running fine.


Solution

  • This is an known issue that should be caused by the recent update of the DockerCompose@0 task. See "DockerCompose@0 uses Docker Compose v2 in v1 compatibility mode".

    There are some other users also have reported the same issue on Developer Community.

    And the support engineer on Developer Community also has reported the issue to the appropriate product team for further investigation. you can follow the tickets on Developer Community and add your comments on the tickets.


    UPDATE:

    According to my further investigation and research, the reason of the issue is with the project name. The update of Compose enforces a stricter naming policy for project names (see here).

    Project names must contain only lowercase letters, decimal digits, dashes, and underscores, and must begin with a lowercase letter or decimal digit. If the base name of the project directory or current directory violates this constraint, alternative mechanisms are available.

    On the DockerCompose@0 task, if you do not provide a value to the field 'projectName' (Project Name), the task will use "$(Build.Repository.Name)" as the default value. It is the name of the source code repository that the pipeline is running.

    enter image description here

    If the repository name contains some characters that do not meet above policy, the task will get failed when using the the updated Compose.

    So, to resolve the issue, it is recommended to provide a compliant name that can meet the naming policy to the DockerCompose@0 task, or update the repository name to meet the policy.