3

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

JCDani
  • 307
  • 7
  • 20
  • The environment variables in the docker-compose are available in (!!) the Container, not during building of the Container ("building" as in: assigning volumes, processing a Dockerfile and commands) – BertC Aug 26 '21 at 06:47
  • @BertC , How can I assign value to variables for volume mounts defined in the docker-compose file ? and why does it work when running manually – JCDani Aug 26 '21 at 07:06
  • You could do this by encapsulating the docker-compose.yml by a script which parses the docker-compose file and replaces the variables with the values. But since you're in a pipeline this might not be possible. – BertC Aug 26 '21 at 07:08

1 Answers1

1

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107