通过 Azure 应用服务的 Terraform 脚本将变量传递给 Docker Compose [英] Passing Variables to Docker Compose via a Terraform script for an Azure App Service
问题描述
我该怎么做
VAR=VAULE docker-compose up
来自使用 Docker Compose 文件构建 Azure 应用服务的 Terraform 脚本
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")}"
}
我正在通过 terraform 成功构建 Azure 应用服务,该服务托管一个推送到 ACR 的 docker 容器.但是,我想将变量传递给我的 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.
docker compose 触发自
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
}
}
我所有的 terraform 都是从 Azure Pipeline 中的部署脚本调用的(这些变量就是在其中设置的)
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"
但是,当 docker-compose.yaml 文件被触发时,这些变量都无法访问.我什至尝试在部署脚本中执行以下操作:
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 片段
docker-compose.yaml snippnet
image: "myacr.azurecr.io/my-container:${VERSION}"
build:
context: ./my-container
dockerfile: Dockerfile-prod
container_name: my-container
有什么想法吗?
推荐答案
我找到了 sed 的解决方法.在我的部署脚本中,我只需使用 sed 替换 docker-compose.yaml 文件中的值,然后用令牌替换它们,以便版本控制保持不变.看起来有点hacky,但它有效:)
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"
这篇关于通过 Azure 应用服务的 Terraform 脚本将变量传递给 Docker Compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!