Skip to content

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

FROM busybox
RUN cd /usr/src/app && git clone git@github.com:lukasmartinelli/hadolint.git

Correct code (with WORKDIR)

FROM busybox
WORKDIR /usr/src/app
RUN git clone git@github.com:lukasmartinelli/hadolint.git

Correct code (with absolute paths)

FROM busybox
RUN cp somedir/somefile /usr/src/app/someDirInUsrSrcApp/

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

Reference