Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WORKDIR $HOME in Dockerfile does not seem to work

I have a Dockerfile that contains two WORKDIR statements (amid other ones) like so:

RUN pwd # reports /root
WORKDIR /tmp
RUN wget ...
RUN tar zxvf ...
RUN cd ... && ; ./configure && make && make install 
RUN sed ...
RUN sed ...
RUN rm ...
RUN rm -fr ...
WORKDIR $HOME
RUN pwd # reports /tmp (instead of /root)

I would expect pwd to be /root (i.e. $HOME for root) afterwards, but it remains at /tmp. Am I making an obvious mistake here? Is there a better way for restoring pwd to its "original" value.

like image 813
Drux Avatar asked Jul 17 '16 16:07

Drux


1 Answers

Seems like the best way would be to explicitly set your own default value so you can be sure it's consistent, like:

ENV HOME /root

WORKDIR $HOME
.. do something in /root ..

WORKDIR /tmp
.. do something else in /tmp ..

WORKDIR $HOME
.. continue back in /root ..

Note:

The WORKDIR instruction can resolve environment variables previously set using ENV. You can only use environment variables explicitly set in the Dockerfile.

https://docs.docker.com/engine/reference/builder/#/workdir

like image 189
ldg Avatar answered Oct 23 '22 19:10

ldg