Use WORKDIR in a Dockerfile

docker

Updated August 29, 2026

WORKDIR sets the current directory for later Dockerfile instructions and for the container's default process:

FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]

This example assumes package-lock.json is included; npm ci requires a lockfile. If the project has no lockfile, use the install command that matches its dependency policy.

If the directory does not exist, Docker creates it. Paths after WORKDIR are resolved relative to the current work directory, so using one explicit absolute directory is easier to review than repeated RUN cd ... commands. WORKDIR affects RUN, COPY, ADD, and the default runtime directory; it does not change the host's working directory.

A Compose working_dir or docker run --workdir can override the runtime working directory. Do not use RUN cd to configure later layers—it applies only to that shell command. Keep application files under a deliberate directory and ensure the runtime user can read them.

Sources

related.

Ruslan Osipov

Ruslan Osipov

About the author