2017-02-01 52 views
9

隨着docker-compose V2環境變量可以通過簡單的設置:泊塢窗 - 撰寫 - 如何逃脫的環境變量

enviroment: 
    - MONGO_PATH=mongodb://db-mongo:27017 

完整docker-compose.yml文件之中:

version: '2' 
services: 
    web: 
    build: . 
    environment: 
     - MONGO_PATH=mongodb://db-mongo:27017 
    ports: 
     - "3000:3000" 
    volumes: 
     - .:/app 
     - /app/node_modules 
    depends_on: 
     - db-mongo 
     - db-redis 
    db-mongo: 
    image: mongo 
    restart: unless-stopped 
    command: --smallfiles 
    ports: 
     - "27017:27017" 
    volumes: 
     - ./data:/data/db 
    [...] 

但是,我怎麼能逃避環境不是普通字符串的變量?

{"database": {"data": {"host": "mongo"}}} 

我想:

NODE_CONFIG=\{"database": \{"data"\: \{"host": "mongo"\}, "session": \{"host": "redis" \}\}\} 
NODE_CONFIG="\{"database": \{"data"\: \{"host": "mongo"\}, "session": \{"host": "redis" \}\}\}" 
NODE_CONFIG='{"database": {"data": {"host": "mongo"}, "session": {"host": "redis" }}}' 

ERROR: yaml.parser.ParserError: while parsing a block mapping in "./docker-compose.yml", line 6, column 9 expected , but found '}' in "./docker-compose.yml", line 6, column 92

回答

16

環境變量(包括姓名),已被完全包裹的單或雙引號內:""''

environment: 
    - 'NODE_CONFIG={"database": {"data": {"host": "mongo"}, "session": {"host": "redis" }}}' 

並使用雙引號:

environment: 
    - 'PORT=3000' 
    - "NODE_CONFIG={\"database\": {\"data\": {\"host\": \"mongo\"}, \"session\": {\"host\": \"redis\" }}}" 

值得注意的是,要注意,使用雙引號"",諸如bash,將允許把變量的環境變量中。

"MY_HOME_ENV_VARIABLE=${HOME}" 
+0

這是否也適用於--env文件,因爲我試過了,它不工作... –