Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 416
dzhi Avatar asked Nov 07 '18 21:11

dzhi


People also ask

Can you use environment variables in Docker compose?

In Docker Compose, IT admins can use environment variables to generalize configurations for different situations, deployment environments and security contexts without editing the main project file(s) manually. Variables can either be passed as command-line arguments -- suitable for only a few parameters -- or via a .

What is Env_file in Docker compose?

The “env_file” configuration option You can pass multiple environment variables from an external file through to a service's containers with the 'env_file' option, just like with docker run --env-file=FILE ... : web: env_file: - web-variables.env.

How do I set an environment variable in Docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

How do you list environment variables in a container?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers.


1 Answers

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

like image 160
Anthon Avatar answered Sep 18 '22 13:09

Anthon