'Passing Variables to Docker Compose via a Terraform script for an Azure App Service

How do I

VAR=VAULE docker-compose up

from a Terraform script building an Azure App Service using a Docker Compose file

 site_config {
    app_command_line = ""
    linux_fx_version = "COMPOSE|${filebase64("../docker-compose.yaml")}"
  }

I'm successfully building an Azure App Service via terraform which hosts a docker container that is pushed to the ACR. However, I would like to pass variables to my docker-compose.yaml file and can't seem to figure out a way to them there.

The docker compose is triggered from


  depends_on = [ null_resource.docker_push ]

  name                = "myapps-apps-appservice"
  location            = "${azurerm_resource_group.mine.location}"
  resource_group_name = "${azurerm_resource_group.mine.name}"
  app_service_plan_id = "${azurerm_app_service_plan.asp.id}"

  site_config {
    app_command_line = ""
    linux_fx_version = "COMPOSE|${filebase64("../docker-compose.yaml")}"
  }

  app_settings = {
    "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false",
    "DOCKER_REGISTRY_SERVER_USERNAME" = azurerm_container_registry.container_registry.admin_username,
    "DOCKER_REGISTRY_SERVER_URL" = azurerm_container_registry.container_registry.login_server,
    "DOCKER_REGISTRY_SERVER_PASSWORD" = azurerm_container_registry.container_registry.admin_password
  }
}

All of my terraform is called from a deployment script from an Azure Pipeline (that's where those variables are set)

terraform apply -auto-approve \
    -var "app_version=$VERSION" \
    -var "client_id=$ARM_CLIENT_ID" \
    -var "client_secret=$ARM_CLIENT_SECRET" \
    -var "tenant_id=$ARM_TENANT_ID" \
    -var "subscription_id=$ARM_SUBSCRIPTION_ID"

However, none of those variables are accessible from within the docker-compose.yaml file when it's triggered. I've even tried doing the following within the deployment script:

export VERSION=1

docker-compose.yaml snippnet

    image: "myacr.azurecr.io/my-container:${VERSION}"
    build: 
      context: ./my-container
      dockerfile: Dockerfile-prod
    container_name: my-container

Any thoughts?



Solution 1:[1]

I found a work around with sed. During my deployment script, I just replace the values in my docker-compose.yaml file using sed and then replace them back with the token so that version control stays the same. Seems a bit hacky, but it works :)

eval "sed -i 's/MY_VERSION/$VERSION/' ../docker-compose.yaml"

cat ../docker-compose.yaml

terraform init 
terraform apply -auto-approve \
    -var "app_version=$VERSION" \
    -var "client_id=$ARM_CLIENT_ID" \
    -var "client_secret=$ARM_CLIENT_SECRET" \
    -var "tenant_id=$ARM_TENANT_ID" \
    -var "subscription_id=$ARM_SUBSCRIPTION_ID"

eval "sed -i 's/$VERSION/MY_VERSION/' ../docker-compose.yaml"

Solution 2:[2]

You can pass variable by setting environment variable in app_settings-block. So add VERSION environment variable in your current app_settings:

  app_settings = {
    "VERSION" = "1"
    "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false",
    "DOCKER_REGISTRY_SERVER_USERNAME" = azurerm_container_registry.container_registry.admin_username,
    "DOCKER_REGISTRY_SERVER_URL" = azurerm_container_registry.container_registry.login_server,
    "DOCKER_REGISTRY_SERVER_PASSWORD" = azurerm_container_registry.container_registry.admin_password
  }

Then, your current docker-compose.yml file should work.

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 bstricks
Solution 2 Tuukka Salmi