How to get proper docker-compose Multiline environment variables formatting? How to get proper docker-compose Multiline environment variables formatting? docker docker

How to get proper docker-compose Multiline environment variables formatting?


In your first example the last element of the first sequence of the document is a plain scalar (i.e. not having single or double quotes) that extends over multiple lines. In a plain scalar newlines are replaced by spaces (and empty lines replaced by a newline).

So if you want newlines within that element you should use (only showing relevant part):

  - WORDPRESS_DB_PASSWORD=xxxxxxxxxxxxxxx  - WORDPRESS_DEBUG=1  - WORDPRESS_CONFIG_EXTRA=      define( 'WP_REDIS_CLIENT', 'predis' );      define( 'WP_REDIS_SCHEME', 'tcp' );      define( 'WP_REDIS_HOST', 'redis' );      define( 'WP_REDIS_PORT', '6379' );      define( 'WP_REDIS_PASSWORD', 'xxxxxxxxxxxxxxx' );      define( 'WP_REDIS_DATABASE', '0' );      define( 'WP_REDIS_MAXTTL', '21600' );      define( 'WP_CACHE_KEY_SALT', 'xx_ ');      define( 'WP_REDIS_SELECTIVE_FLUSH', 'xx_ ');      define( 'WP_AUTO_UPDATE_CORE', false );volumes:  - ./wordpress:/var/www/html

or:

  - WORDPRESS_DB_PASSWORD=xxxxxxxxxxxxxxx  - WORDPRESS_DEBUG=1  - |    WORDPRESS_CONFIG_EXTRA=    define( 'WP_REDIS_CLIENT', 'predis' );    define( 'WP_REDIS_SCHEME', 'tcp' );    define( 'WP_REDIS_HOST', 'redis' );    define( 'WP_REDIS_PORT', '6379' );    define( 'WP_REDIS_PASSWORD', 'xxxxxxxxxxxxxxx' );    define( 'WP_REDIS_DATABASE', '0' );    define( 'WP_REDIS_MAXTTL', '21600' );    define( 'WP_CACHE_KEY_SALT', 'xx_ ');    define( 'WP_REDIS_SELECTIVE_FLUSH', 'xx_ ');    define( 'WP_AUTO_UPDATE_CORE', false );volumes:  - ./wordpress:/var/www/html

Using |- instead of | excludes the final newline from that element. What you tried ( WORDPRESS_CONFIG_EXTRA: | ) is something completely different, as you split the single scalar element into a mapping with a single key-value pair.

Although the above load as string values with embedded newlines, it can still happen that the processing done by docker-compose, in particular passing things to a shell, can change the newlines into spaces.

I have also used programs where, if you might have to escape the newline for the "folllowing" processing by ending each line with a backslash (\)


I prefer use a slight different syntax and try using >. This. solution works pretty well if you need to have a json in your env variables. There are many ways to have a multiline strings in YAML.

version: '2'services:  wordpress:    container_name: 'wordpress'    image: 'wordpress:php7.2-fpm-alpine'    user: 1001:1001    environment:      WORDPRESS_DB_HOST: mysql      WORDPRESS_DB_USER: something      WORDPRESS_DB_NAME: something      WORDPRESS_DB_PASSWORD: xxxxxxxxxxxxxxx      WORDPRESS_DEBUG: 1      WORDPRESS_CONFIG_EXTRA: >          define( 'WP_REDIS_CLIENT', 'predis' );          define( 'WP_REDIS_SCHEME', 'tcp' );          define( 'WP_REDIS_HOST', 'redis' );          define( 'WP_REDIS_PORT', '6379' );          define( 'WP_REDIS_PASSWORD', 'xxxxxxxxxxxxxxx' );          define( 'WP_REDIS_DATABASE', '0' );          define( 'WP_REDIS_MAXTTL', '21600' );          define( 'WP_CACHE_KEY_SALT', 'xx_ ');          define( 'WP_REDIS_SELECTIVE_FLUSH', 'xx_ ');          define( 'WP_AUTO_UPDATE_CORE', false );      CONFIG_ABC: >          {            "database": {               "catalog": {                   "credentials": {                       "username": "scott",                       "password": "tiger",                       "datbase": "catalog",                       "host": "gn.dmfkd.lan"                    }                }            }          }      CONFIG_DEF: >          {            "urlRegex": "/.*",            "script": {              "scriptPath": "example-python-app.py"            },            "runtime": "python27",            "threadsafe": true,          }    volumes:      - ./wordpress:/var/www/html      - ./logs/php:/var/logs/php      - ./config/php/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro    networks:      - frontend      - backend    restart: always    depends_on:      - mysql