Skip to main content

DL3003: Use WORKDIR to switch directories

Warning Efficiency

Why This Matters

Using `cd` inside RUN instructions does not persist across layers because each RUN starts in the WORKDIR. Chaining `cd dir && command` works but makes the Dockerfile harder to read and maintain. Developers often forget that `cd` in one RUN does not affect the next, which leads to path-related bugs. WORKDIR is the idiomatic way to set the working directory and it persists across all subsequent instructions.

How to Fix

Replace cd with a WORKDIR instruction

Before (incorrect)

RUN cd /app && npm install

After (correct)

WORKDIR /app
RUN npm install

Rule Details

Rule Code
DL3003
Severity
Warning
Category
Efficiency

Related Rules