2016-11-15 99 views
17

我有,當我試圖評估我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將其解釋爲錯誤的字符串插值?

+0

這不是解釋該字符串的YAML解析器。 YAML不知道'$ {}'。解釋是由'docker-compose'完成的,並且是用Python編寫的,所以標籤ruby也不合適。 – Anthon

+0

我刪除了這是一個YAML問題的錯誤假設。它導致至少一個其他人[被誤導並浪費時間](https://stackoverflow.com/questions/45312806/how-to-escape-a-sign-in-an-ansible-host-vars-file) – Anthon

回答

27

你擊中docker-compose變量substition,這是有據可查的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年。

9

通過複製%字的建議中this post

它需要一個雙美元符號$$找到了答案。

所以,我需要"$${Time.now}",其計算結果爲"${Time.now}"