Docker Community Forums

Share and learn in the Docker community.

CMD and Enviroment Variables not working?

(Project487) #1

I am having issues append an environment variable in my Dockerfile to the ENTRYPOINT. Am using Docker 17.12 on Ubuntu 16.04

Due to the Dockerfile having sensitive data I will try and show a similar example:

FROM ubuntu:latest
ENV test_var hello

ENTRYPOINT ["keyword"] 

CMD ["sh", "-c", "echo $test_var"]

I got the CMD execution from: and

In my mind, when running:

sudo docker run -it nameOfImage 

keyword should be executed with $test_var being appended. Either of the following two should work:

keyword $test_var

keyword hello

However it is just not working for me. The environment variable is subsituting correctly when using RUN:

RUN echo $test_var

However, CMD fails to work with it. Any suggestions? Thank you.

(Project487) #2

Bump. Sorry to be a nuisance but does anyone have any suggestions? Thanks again.

(Sam) #3

see this comment from the entrypoint command

Note: _Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME._ If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

(Project487) #4

Thanks for the reply. What I hoped to achieve was have ENTRYPOINT execute a program and then CMD would include dynamic parameters in the form of enviroment variables.

With regards to documentation, I’ve used that format but it still doesn’t work. That paragraph corresponds to CMD as well I think.

(Sam) #5

I understand… I had to change the keyword script like this

some to prove the parms are coming across

#!/bin/bash -x

echo parms equal $@

for i in $*; do 
   echo $i 

export test_var1=$test_var

echo executing parms
# use eval to execute the passed in command string
v=eval   $t     
echo done executing parms, results=$v