As you've mentioned that you need to use the variable during build time only, this should do the job:
Dockerfile:
FROM node:alpine
ENV MY_ENCODED_VALUE "bXkgbmFtZSBpcyByYWtpYgo="
RUN echo $MY_ENCODED_VALUE | base64 -d > /root/temp
RUN MY_DECODED_VALUE=$(cat /root/temp); echo "Output: $MY_DECODED_VALUE"
Output:
$ docker build -t test .
Sending build context to Docker daemon 2.56kB
Step 1/4 : FROM node:alpine
---> bcfeabd22749
Step 2/4 : ENV MY_ENCODED_VALUE "bXkgbmFtZSBpcyByYWtpYgo="
---> Using cache
---> 81084f4be2e4
Step 3/4 : RUN echo $MY_ENCODED_VALUE | base64 -d > /root/temp
---> Using cache
---> b8ad3a100746
Step 4/4 : RUN MY_DECODED_VALUE=$(cat /root/temp); echo "Output: $MY_DECODED_VALUE"
---> Running in c9c41b92dee0
Output: my name is rakib
Removing intermediate container c9c41b92dee0
---> acfcd422a8ed
Successfully built acfcd422a8ed
Successfully tagged testing:latest
Note:
1) The RUN
instruction in the last line of my Dockerfile
is for the latter part i.e., the echo
command. Assigning variable [MY_DECODED_VALUE=$(cat /root/temp)
] just before the echo
command ensures that the variable gets set in the same layer where you want to consume it.
2) The way i have used variable assignment, it will not behave as you would expect from an ENV
instruction i.e., it will not be available for use across layers. If you want to consume the variable in multiple layers, then you will have to use RUN MY_DECODED_VALUE=$(cat /root/temp); <your-command-that-uses-the-variable>
, wherever applicable. Not an elegant one but that's how it works with my solution.