1

I am using Docker 17.04.0-ce, build 4845c56 with docker-compose 1.12.0, build b31ff33 on Ubuntu 16.04.2 LTS. I simply want to pass an environment variable and display it from my script running in a container. I am doing this according to the documentation https://docs.docker.com/compose/compose-file/#environment . The problem is that the variable is not passed to the container.

My docker-compose.yml file:

env-file-test:
  build: .
  dockerfile: Dockerfile
  environment:
    - DEMO_VAR

My Dockerfile:

FROM alpine
COPY docker-start.sh /
CMD ["/docker-start.sh"]

And the docker-start.sh file:

#!/bin/sh
echo "DEMO_VAR Var Passed in: $DEMO_VAR"

I try to set the variable in my current terminal session and pass it to the container:

$ export DEMO_VAR=aabbdd
$ echo $DEMO_VAR
aabbdd
$ sudo docker-compose up
Starting envfiletest_env-file-test_1
Attaching to envfiletest_env-file-test_1
env-file-test_1  | DEMO_VAR Var Passed in: 
envfiletest_env-file-test_1 exited with code 0

So you can see that the variable DEMO_VAR is empty!

I also tried using variables in docker-compose.yml like this: DEMO_VAR=${DEMO_VAR} but then when I run sudo docker-compose up, I get a warning: "WARNING: The DEMO_VAR variable is not set. Defaulting to a blank string.".

What am I doing wrong? What should I do to pass the variable to the container?

2 Answers 2

14

I found a solution. Answering my own question...

The problem was with the sudo command. It turned out that it does not pass environment variables by default. There are some possible solutions:

  1. Use sudo -E. Demo:

    $ export DEMO_VAR=aabbdd
    $ echo $DEMO_VAR
    aabbdd
    $ sudo -E docker-compose up
    env-file-test_1  | DEMO_VAR Var Passed in: aabbdd
    
  2. Use sudo VAR=value:

    sudo DEMO_VAR=$DEMO_VAR docker-compose up
    
  3. Add environment variables to the sudoers file (https://stackoverflow.com/a/8636711)

  4. Use docker without sudo (https://askubuntu.com/questions/477551/how-can-i-use-docker-without-sudo)

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

Comments

-2

you should use ENV in your Dockerfile, and avoid export.

See the doc

https://docs.docker.com/engine/reference/builder/#env

5 Comments

Could you give an example how to use ENV to pass the environment variable from the current terminal?
You use ENV in your Dockerfile, with a line such as ENV demo_var aabbdd or other syntax ENV demo_var=aabbdd
But I don't want to set the value in the Dockerfile. The DEMO_VAR is only an example, but in a real-world project it can contain a server-specific configuration variable or even a password. I want to pass it from the current session (current terminal).
"The ARG instruction defines a variable that users can pass at build-time"... but I want to pass a variable at run-time, not build-time. BTW: I've just found a solution to my problem. I posted it as an answer.
at run time, you have docker run -e see the doc docs.docker.com/engine/reference/run/#env-environment-variables

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.