Docker Community Forums

Share and learn in the Docker community.

Set Environment variable through Dockerfile

How to set environment variable value equal to output of given command:

Example:-
RUN export JAVA_HOME=`sudo update-java-alternatives -l | awk ‘{print $3}’`

This command executes and set environment variable “JAVA_HOME” for the current container during build process, however because of Issue 684 final image do not have JAVA_HOME.

Few thoughts:
Is it possible to get return value from RUN?
Is it possible to execute command through ENV?

Any comments/suggestions?

You could define an entrypoint.sh like in this Docker Project.


In Dockerfile set this entrypoint.sh as ENTRYPOINT.
Then your Environment is set as you wished.

It’s not possible from a RUN or ENV Command.
Maybe there is a workaround for the RUN Command. You can do a trick like
RUN echo "JAVA_HOME=$(sudo update-java-alternatives -l | awk '{print $3}')" >> ~/.profile. But this also requires that you have a bash or a sh as ENTRYPOINT.

IMHO JAVA_HOME is a solution for supporting multiple different Java installations on a PC. In Docker you probably don’t want this. You just want to support a particular version you carefully picked before. So you could install this java directly to /usr/bin or some other prominent place.

As a general rule, you should assume containers won’t run in a way that uses shell dotfiles or anything in any particular user’s home directory. An image-global Java installation, or failing that, an entrypoint script, is almost certainly the right answer here.

@dmaze : sure. I marked this as workaround. That is the most negative word I could think about. :slight_smile: