Example 1:-
<code>FROM ubuntu:latest
SHELL ["/bin/bash", "-c"]
RUN echo "export MY_VARIABLE=my_value" >> ~/.profile
RUN cat ~/.profile
RUN "source ~/.profile && echo env variable4: $MY_VARIABLE
sudo docker build --no-cache -t hello-image --progress plain .
o/p:-
variable4:my_value
</code>
<code>FROM ubuntu:latest
SHELL ["/bin/bash", "-c"]
RUN echo "export MY_VARIABLE=my_value" >> ~/.profile
RUN cat ~/.profile
RUN "source ~/.profile && echo env variable4: $MY_VARIABLE
sudo docker build --no-cache -t hello-image --progress plain .
o/p:-
variable4:my_value
</code>
FROM ubuntu:latest
SHELL ["/bin/bash", "-c"]
RUN echo "export MY_VARIABLE=my_value" >> ~/.profile
RUN cat ~/.profile
RUN "source ~/.profile && echo env variable4: $MY_VARIABLE
sudo docker build --no-cache -t hello-image --progress plain .
o/p:-
variable4:my_value
Example 2:-
<code>FROM ubuntu:latest
RUN echo "export MY_VARIABLE=my_value" >> ~/.profile
RUN cat ~/.profile
RUN /bin/bash -c "source ~/.profile && echo env variable4: $MY_VARIABLE
sudo docker build --no-cache -t hello-image --progress plain .
o/p:-
variable4:
</code>
<code>FROM ubuntu:latest
RUN echo "export MY_VARIABLE=my_value" >> ~/.profile
RUN cat ~/.profile
RUN /bin/bash -c "source ~/.profile && echo env variable4: $MY_VARIABLE
sudo docker build --no-cache -t hello-image --progress plain .
o/p:-
variable4:
</code>
FROM ubuntu:latest
RUN echo "export MY_VARIABLE=my_value" >> ~/.profile
RUN cat ~/.profile
RUN /bin/bash -c "source ~/.profile && echo env variable4: $MY_VARIABLE
sudo docker build --no-cache -t hello-image --progress plain .
o/p:-
variable4:
Why in first example, “$MY_VARIABLE” got value “my_value” but in second example it is empty string?