Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare env variable which value include space for docker/docker-compose

I have an environment variable defined in a file passed in via --env-file like this:
TEST_VAR=The value

Does anybody know whether this is legal? should I place " around the value for this to be interpreted as needed in docker? Thanks

EDIT: Quotation marks will not be a good solution as it is will be part of the val see reference here.

like image 816
JavaSa Avatar asked Sep 10 '17 13:09

JavaSa


People also ask

Can environment variable have space?

Most programs separate their command line arguments with a space. But the PATH environment variable doesn't use spaces to separate directories. It uses semicolons.

Can Docker Compose use env variables?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.

Does Docker compose override environment variables?

But docker-compose does not stop at the . env and the host's current environment variables. It's cool that you can simply override values of your . env file, but this flexibility is can also be the source of nasty bugs.


3 Answers

Lets see the result running the following compose file:

version: "3"

services:
    service:
        image: alpine
        command: env
        env_file: env.conf

env.conf:

TEST_VAR1=The value
TEST_VAR2="The value2"

docker-compose up Result:

service_1 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
service_1  | TEST_VAR2="The value2"
service_1  | TEST_VAR1=The value
service_1  | HOME=/root

Therefore, it is legal to have spaces in the env value.

like image 102
yamenk Avatar answered Oct 08 '22 21:10

yamenk


You can escape the space with a \:

TEST_VAR=The\ value

Edit: This is how I pass them when starting the container (i.e. docker run -e TEST_VAR=The\ value hello-world). If you're using docker-compose or an env file, see the answer by @yamenk.

like image 18
rickdenhaan Avatar answered Oct 08 '22 23:10

rickdenhaan


In Dockerfile use doublequotes, do not use singlequotes because they do not expand variables inside, excerp from passing buildargs/envs to dockerfile and into a python script below:

ARG HOST="welfare-dev testapi"
ENV HOST "${HOST}"
ARG SITENAME="Institusjon"
ENV SITENAME "${SITENAME}"
RUN cd ${TESTDIR}/sensiotools/sensiotools && cd test && \
  ./testapi-events.py --activate --sitename="${SITENAME}" --host="${HOST}" --dbcheck --debug --wait=0.5 && \
  ./testapi-events.py --deactivate --sitename="${SITENAME}" --host="${HOST}" --dbcheck --debug
like image 4
MortenB Avatar answered Oct 08 '22 21:10

MortenB