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?

JustAC0der
  • 2,871
  • 3
  • 32
  • 35

2 Answers2

13

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)

Community
  • 1
  • 1
JustAC0der
  • 2,871
  • 3
  • 32
  • 35
-2

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

See the doc

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

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • Could you give an example how to use `ENV` to pass the environment variable from the current terminal? – JustAC0der Apr 21 '17 at 06:34
  • You use ENV in your Dockerfile, with a line such as `ENV demo_var aabbdd` or other syntax `ENV demo_var=aabbdd` – user2915097 Apr 21 '17 at 06:55
  • 1
    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). – JustAC0der Apr 21 '17 at 06:57
  • "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. – JustAC0der Apr 21 '17 at 07:11
  • at run time, you have `docker run -e` see the doc https://docs.docker.com/engine/reference/run/#env-environment-variables – user2915097 Apr 21 '17 at 07:25