61

In terms of the way Docker works, is there any difference between RUN cd / and WORKDIR / inside a Dockerfile?

0

1 Answer 1

110

WORKDIR / changes the working directory for future commands.
RUN cd / has no impact on the subsequent RUNs.

Each RUN command runs in a new shell and a new environment (and technically a new container, though you won't usually notice this). The ENV and WORKDIR directives before it affect how it starts up. If you have a RUN step that just changes directories, that will get lost when the shell exits, and the next step will start in the most recent WORKDIR of the image.

FROM busybox
WORKDIR /tmp
RUN pwd       # /tmp

RUN cd /      # no effect, resets after end of RUN line
RUN pwd       # still /tmp

WORKDIR /
RUN pwd       # /

RUN cd /tmp && pwd  # /tmp
RUN pwd       # /

(For the same reason, RUN export doesn't do anything that outlives the current Dockerfile instructions, and RUN . or the non-standard RUN source won't cause environment variables to be set.)

Sign up to request clarification or add additional context in comments.

1 Comment

Does absolutely nothingto the subsequent RUNs, not the current one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.