当我尝试评估我的 docker-compose.yml
文件时,我有一个 YAML 标量抛出以下错误:
ERROR: Invalid interpolation format for "environment" option in service "time_service": "${Time.now}"
YAML:
---
version: '2'
services:
time_service:
build: "."
environment:
TIME: "${Time.now}"
如何保持与写入相同的字符串输出,但避免让 docker-compose 将其解释为错误的字符串插值?
最佳答案
You can use a
$$
(double-dollar sign) when your configuration needs a literal dollar sign.
您正在使用 docker-compose
变量替换,这是有详细记录的 here :
Both
$VARIABLE
and${VARIABLE}
syntax are supported. Extended shell-style features, such as${VARIABLE-default}
and${VARIABLE/foo/bar}
, are not supported.You can use a
$$
(double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a$$
allows you to refer to environment variables that you don’t want processed by Compose.
docker-compose
是用 Python 编写的,如 github 上所示。 ,在很多程序中都可以找到获得特殊字符原始含义的加倍机制,我自己在编程时需要使用这个机制,早在 1984 年。
关于escaping - 如何在 docker compose 文件中转义 $ 美元符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40619582/