beginnerDockerfile Instructions & Multi-Stage Builds

Why might WORKDIR be safer than using RUN cd?

Each RUN instruction executes in its own subshell/layer; a cd inside one RUN does not persist to the next RUN instruction. WORKDIR, by contrast, is a Dockerfile-level setting that persists across all subsequent instructions (RUN, CMD, ENTRYPOINT, COPY, ADD) for the rest of that build stage, and it also creates the directory if it doesn't already exist.

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full DevOps Mastery library.

Why might WORKDIR be safer than using RUN cd?

Next Step

Continue to docker stop always takes 10 seconds even though the app shuts down instantly when killed manually← Back to all Docker questions