Drux
Drux

Reputation: 12670

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.

Upvotes: 14

Views: 18759

Answers (1)

ldg
ldg

Reputation: 9402

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

Upvotes: 30

Related Questions