Skip to main content
Use WORKDIR to switch to a directory.

Description

Only use cd in a subshell. Most commands can work with absolute paths. Docker provides the WORKDIR instruction if you really need to change the current working directory. When executed in a subshell, cd only affects the single RUN instruction, not any subsequent instructions. This can be an advantage over WORKDIR which affects all subsequent instructions.

Examples

Problematic code

Correct code (with WORKDIR)

Correct code (with absolute paths)

Auto-fix

Splits RUN with cd into WORKDIR + RUN instructions. Removes redundant mkdir commands before cd targets.

Reference