如何在 docker compose 文件中转义 $ 美元符号? [英] How can I escape a $ dollar sign in a docker compose file?
问题描述
当我尝试评估我的 docker-compose.yml
文件时,我有一个 YAML 标量抛出以下错误:
I have a YAML scalar that is throwing the following error when I try to evaluate my docker-compose.yml
file:
错误:环境"选项的插值格式无效服务time_service":${Time.now}"
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 将其解释为错误的字符串插值?
How can I maintain the same string output as written, but avoid having the docker-compose interpret it as faulty string interpolation?
推荐答案
当您的配置需要时,您可以使用
$$
(双美元符号)字面上的美元符号.
You can use a
$$
(double-dollar sign) when your configuration needs a literal dollar sign.
您正在使用 docker-compose
变量替换,这是有据可查的 这里:
You are hitting the docker-compose
variable substitution, which is well documented here:
支持 $VARIABLE
和 ${VARIABLE}
语法.不支持扩展的 shell 样式功能,例如 ${VARIABLE-default}
和 ${VARIABLE/foo/bar}
.
Both
$VARIABLE
and${VARIABLE}
syntax are supported. Extended shell-style features, such as${VARIABLE-default}
and${VARIABLE/foo/bar}
, are not supported.
当您的配置需要文字美元符号时,您可以使用 $$
(双美元符号).这也可以防止 Compose 插入值,因此 $$
允许您引用不想由 Compose 处理的环境变量.
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年.
docker-compose
is written in Python, as you see on github, the doubling mechanism to get the original meaning of special characters can be found in many programs, I needed to use this myself, while programming, as far back in 1984.
这篇关于如何在 docker compose 文件中转义 $ 美元符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!