21

I made a docker-compose.yaml for my Wordpress stack using official Wordpress image and I want to add some custom constants in wp-config.php file automatically.

By following official image instructions I end up with this:

### Web Application
  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 );
    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

Everything works but my OCD can't rest until I figure out why generated wp-config.php looks like this: WORDPRESS_CONFIG_EXTRA constants joined in one line:

// 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_');

..instead of like this, formatted with each constant being on new line which is much more readable:

// 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_');

Can anyone guide me on how multiline environment variables are handled in docker-compose file, specifically for WORDPRESS_CONFIG_EXTRA variable?

I tried WORDPRESS_CONFIG_EXTRA: | and WORDPRESS_CONFIG_EXTRA: |- but none worked the way I think it should.

Anthon
  • 69,918
  • 32
  • 186
  • 246
dzhi
  • 1,534
  • 2
  • 18
  • 29

2 Answers2

29

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 (\)

Anthon
  • 69,918
  • 32
  • 186
  • 246
14

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
freedev
  • 25,946
  • 8
  • 108
  • 125