How to resolve the variable substitution for volume mount issue in docker-compose file when deploying through azure pipeline?

Viewed 468

I have a docker-compose file

version: "3.6"
services:
    web:
        image: p21d01.azurecr.io/web1:latest
        env_file:
          - ./test.env
        volumes:
          - "${BASE_MOUNT_PATH}/config:${BASE_FILEPROCESSING_PATH}"

The docker compose file has a env file as well that has the value of the file mount variables

test.env

####Mounting#######
BASE_MOUNT_PATH=/opt/docker/folder1
BASE_FILEPROCESSING_PATH=/opt/processing

I am using the below code in the azure pipeline yaml to run the containers

  - task: DockerCompose@0
    displayName: Run a Docker Compose command
    inputs:
      containerregistrytype: 'Container Registry'
      action: Run a Docker Compose command
      #azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
      #azureContainerRegistry: $(azureContainerRegistry)
      dockerComposeFile: '$(Pipeline.Workspace)/s/docker_compose/docker-compose.yaml'
      dockerComposeCommand: '--env-file $(Pipeline.Workspace)/s/docker_compose/test.env up -d'

I get the below error when running the pipeline . The above code though creates the container successfully , it throws the below error and no volume mount happens

##[error]The BASE_MOUNT_PATH variable is not set. Defaulting to a blank string.
##[error]The BASE_FILEPROCESSING_PATH variable is not set. Defaulting to a blank string.

The above code works fine when manually running it. but when using the above code , the above mentioned error is displayed. Please help in resolving this error

1 Answers

You need to put them as env mapping as below:

- task: DockerCompose@0
    displayName: Run a Docker Compose command
    inputs:
      containerregistrytype: 'Container Registry'
      action: Run a Docker Compose command
      #azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
      #azureContainerRegistry: $(azureContainerRegistry)
      dockerComposeFile: '$(Pipeline.Workspace)/s/docker_compose/docker-compose.yaml'
      dockerComposeCommand: '--env-file $(Pipeline.Workspace)/s/docker_compose/test.env up -d'
    env:
      BASE_MOUNT_PATH:'/opt/docker/folder1'
      BASE_FILEPROCESSING_PATH:'/opt/processing'

it will create environment variables on agent which build your code.

Related