hadolint/DL3003¶
Use WORKDIR to switch to a directory.
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Best Practice |
| Default | Enabled |
| Auto-fix | Yes (--fix) |
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.
# Before
RUN cd /tmp && git clone ... && cd repo && make
# After (with --fix)
WORKDIR /tmp
RUN git clone ...
WORKDIR repo
RUN make