-
Notifications
You must be signed in to change notification settings - Fork 18.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dockerfile CMD doesn't understand ENV variables #5509
Comments
Have you tried this way?
|
Yes. It doesn't work:
|
I can confirm this. Using a Dockerfile with this
And it doesn't work on the build of the image. However, it work when you enter through /bin/bash (docker run [image] /bin/bash) and type:
Will print:
I have also tried:
With no luck. I'll keep trying to work around this issue |
Try The explanation is that the shell is responsible for expanding environment variables, not Docker. When you use the JSON syntax, you're explicitly requesting that your command bypass the shell and be execed directly. |
Confirmed that the form Might be nice to add a note about this to the documentation. |
Closes moby#5509 Signed-off-by: Doug Davis <dug@us.ibm.com>
According to (moby/moby#5509) cmd does not work with env vars unless entrypoint is calling shell. Due to dictionary usage instead of strings, we get raw mysqld_safe instead of /bin/sh -c ../mysqld_safe. Not a big deal, just needed to be accounted for.
Closes moby#5509 Signed-off-by: Doug Davis <dug@us.ibm.com>
And based on @sfitts logic, as CMD is passed to the ENTRYPOINT as an argument, the following is possible: Dockerfile:
start.sh
|
I'd like this issue to be reopened. The "answer" as provided above is merely a workaround, punting the expansion of ENV variables to the shell. I can immediately think of several scenarios in which this workaround is insufficient. EDIT: Please see issue #34772, which I just created as a correlation for ARG directives. The use cases I mention there have possible overlap with the issues with ENV. |
+1 to @dejayc |
workaround for moby/moby#5509
workaround for moby/moby#5509
In case this may be useful for anyone, I wrote a small Go script to achieve this. Wrapping with shell script works but does not forward OS signals to the process. |
+1 many of us are using the ENTRYPOINT ["docker-entrypoint.sh"] CMD ["something", "${VARIABLE"] pattern where this fails. The last line of docker-entrypoint.sh is typically |
Another workaround is to move the command into the script. And in there you'll get variable expansion. |
I've tried
|
@andho Yes. I tried it a couple weeks ago and the env vars are always available within the app. I've been trying to remember what I was getting stuck on and can't. It for sure would have been a 3rd party binary I was trying to build into a container, so maybe it was just doing something odd and I didn't notice. I'm curious how the app ends up with access to the expanded env vars if the shell is responsible for expanding them. Is that a Go specific thing? When I tested it I noticed that if I use |
@ryanjaeb The apps will have access to the environment variables in the environment it's run within. The shell is also an app, which is used to call other apps. So the apps called from within the shell also has access to env vars. But most apps take input/config through arguments instead of env vars, and that's where variable expansion is needed. Hope the explanation is clear. |
Write the command without use the args array: ie |
Not sure if it's still relevant, but in order to use env vars in CMD I do this:
|
The simplest solution I found was ARG envarg
ENV ENTERPRISE_BUILD ${envarg}
CMD /usr/local/myapp local --no-autoreload --host 0.0.0.0 --stage ${ENTERPRISE_BUILD} and docker build using docker build --build-arg $envarg=staging -f ./Dockerfile -t local/test . I think with and without |
@rjshrjndrn of course Using shell form has a main drawback of no running your software as PID 1. |
I'm using tiny as my init daemon.. So I hope I'm good there. |
For python docker container, use this format:
|
This way shell is still used to execute process. ENV var=hello
CMD python a.py -p ${var} Read this piece: https://docs.docker.com/engine/reference/builder/#cmd |
Using the string form or |
@beporter you need to add CMD exec foo $BAR |
Apparently doesn't work for me :(
|
This issue is not closed. Please reopen and fix it! |
I have the same problem:
Using: |
After coming across this issue in search of a solution and trying some of the workarounds, I eventually settled on:
|
Yet another workaround with shell's
# entrypoint.sh
CMD=$(eval echo $@)
exec $CMD Note this will probably defeat a user attempt to pass certain strings unevaled to your container. E.g. Also, obviously, do not use this approach with untrusted input (think about |
Here's what i did for an image running a node program to which i needed to pass a flag.
In my case i start as root and set up a start.sh script which runs node with the env variable being a flag. Then i switch to my user and run the shell script. |
Similar to #1136.
I want to be able to say
CMD [ "$CATALINA_HOME/bin/catalina.sh", "run"]
The text was updated successfully, but these errors were encountered: