Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile ENV backtick interpolation [duplicate]

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 234
Thom Thom Thom Avatar asked Jun 26 '26 01:06

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 91
Stephen Ng'etich Avatar answered Jun 27 '26 16:06

Stephen Ng'etich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!