Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile - Defining an ENV variable with a dynamic value

I want to update the PATH environment variable with a dynamic value. This is what I've tried so far in my Dockerfile:

...
ENV PATH '$(dirname $(find /opt -name "ruby" | grep -i bin)):$PATH'
...

But export shows that the command was not interpreted:

root@97287b22c251:/# export
declare -x PATH="\$(dirname \$(find /opt -name \"ruby\" | grep -i bin)):\$PATH"

I don't want to hardcode the value. Is it possible to achieve it?

Thanks

like image 523
Thom Thom Thom Avatar asked Jan 18 '16 16:01

Thom Thom Thom


1 Answers

we can't do that, as that would be a huge security issue. Meaning you could run and environment variable like this

 ENV PATH $(rm -rf /)

However, you can pass the information through a --build-arg (ARG) when building an image;

ARG DYNAMIC_VALUE 
ENV PATH=${DYNAMIC_VALUE:-unknown}
RUN echo $PATH

and build an image with:

> docker build --build-arg DYNAMIC_VALUE=$(dirname $(find /opt -name "ruby" | grep -i bin)):$PATH .

Or, if you want to copy information from an existing env-var on the host;

> export DYNAMIC_VALUE=foobar
> docker build --build-arg DYNAMIC_VALUE .
like image 105
Stephen Ng'etich Avatar answered Oct 12 '22 00:10

Stephen Ng'etich