Passing a JSON file as environment variable in Docker
Updated answer
You mentioned that you use the docker run
command in a systemd unit file. A systemd ExecStart
options is not started in a shell. Environment variable substitution is supported by name. Also see the documentation on this:
Basic environment variable substitution is supported. Use "
${FOO}
" as part of a word, or as a word of its own, on the command line, in which case it will be replaced by the value of the environment variable including all whitespace it contains, resulting in a single argument.
The doc also says that StartExec
is not executed in a shell:
This syntax is intended to be very similar to shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood. Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported. [...] Note that shell command lines are not directly supported.
However, you can use ExecStart
to start a shell and then pass a command using the -c
flag (you still need to quote the variable as mentioned in my original answer below):
ExecStart=/bin/bash -c "docker run -e \"TEMP_CONFIG=$(</etc/config.json)\" ..."
Original answer
Your JSON string contains spaces, and without quoting your shell will interpret everything after the first space as subsequent arguments. So TEMP_CONFIG=$(cat /etc/config.json)
is essentially equivalent to:
--env TEMP_CONFIG={ "conf" : { "...
In this case, the TEMP_CONFIG
environmant variable will have the value {
, and docker run
will assume "conf"
to be the next argument (in this case, the image name).
Solution: Quote your bash variables:
--env "TEMP_CONFIG=$(cat /etc/config.json)"
Also, don't use cat
when you don't have to:
--env "TEMP_CONFIG=$(</etc/config.json)"