Set Multiline Environment Variable with Dockerfile

How can I pass a multi-line variable to a docker container?

The YAML syntax is correct. The shell command wasn't:

echo "$KEY"

prints the string with newlines.

Dockerfile: Setting multiple environment variables in single line

There are two formats for specifying environments. If you need single variable then you below format

ENV X Y

This will assign X as Y

ENV X Y Z

This will assign X as Y Z

If you need to assign multiple environment variables then you use the other format

ENV X=Y Z=A

This will assign X as Y and Z as A. So your Dockerfile should be

FROM alpine:3.6
ENV RUBY_MAJOR=2.4 \
RUBY_VERSION=2.4.1 \
RUBY_DOWNLOAD_SHA256=4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654 \
RUBYGEMS_VERSION=2.6.12 \
BUNDLER_VERSION=1.15.3

RUN env

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

How to output a multiline string in Dockerfile with a single command

There is another question similar to this with a solution:
How to write commands with multiple lines in Dockerfile while preserving the new lines?

The answer to this question is more particular in how to use multiline strings in bash rather than how to use Docker.

Following this solution you may accomplish what you want to do as shown below:

RUN echo $' \n\
*****first row ***** \n\
*****second row ***** \n\
*****third row ***** ' >> /home/myfile

More info about this leading dollar sign here:
How does the leading dollar sign affect single quotes in Bash?

Note that this syntax relies on the run command using /bin/bash, not /bin/sh.



Related Topics



Leave a reply



Submit