35

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.

4 Answers 4

29

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I meant values in docker env file, not docker-compose file environment section
@JavaSa updated answer. The result is the same for both env_file and environment.
Ok, does this apply also to docker-compose2?
What if I'm not using an env.conf? Do quotes still work in the yml file itself?
based on my experience @void.pointer, it doesn't work in the docker-compose.yml
|
21

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.

1 Comment

This worked for me; so, I'm not sure what the other commentators are talking about! -- and the other answers are complete nonsense, and not related to dockerfiles.
4

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

Comments

0

My case with docker-compose, if that can help. I couldn't make use of the suggestions in the other answers.

For a variable in volumes, I could use the .env file:

# .env
LOCAL_DIR=/local/path

while for a variable with spaces (for https://github.com/wolfcw/libfaketime in my case) I had to use command line: FAKETIME_ARG="@2021-02-11 13:23:02" docker-compose up.

The resulting docker-compose file (note the ${} just for LOCAL_DIR) :

# docker-compose.yml
services:
  myservice:
    build:
      context: ./path/to/dir/of/Dockerfile
      args:
        - FAKETIME_ARG
    volumes:
      - ${LOCAL_DIR}:/path/in/container

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.