Docker WORKDIR and docker run -w
Answer: WORKDIR sets the directory used by later RUN, CMD, ENTRYPOINT, COPY, and ADD steps. If it does not exist, it is created. If a relative path is provided, it will be relative to the previous WORKDIR. docker run -w can set the working directory; if the directory doesn't already exist in the container, it's created.
Examples
FROM python:3.13-slim
WORKDIR /app
COPY . .
CMD ["python", "app.py"]
docker run --rm -w /tmp alpine pwd
Rules
- The WORKDIR instruction can be used multiple times in a Dockerfile.
- Environment variables previously set with ENV can be used in the path.
See also
Claims
- id: C1 claim: WORKDIR sets the working directory for later build steps. source: S1 evidence: "The WORKDIR instruction sets the working directory"
- id: C2 claim: A relative WORKDIR is resolved against the previous one. source: S1 evidence: "If a relative path is provided, it will be relative"
- id: C3 claim: A missing runtime workdir is created in the container. source: S2 evidence: "If the directory doesn't already exist in the container"
Sources
- [S1] Dockerfile reference. Docker docs. https://docs.docker.com/reference/dockerfile/ . License: Apache-2.0. Accessed 2026-11-18.
- [S2] Running containers. Docker docs. https://docs.docker.com/engine/reference/run/ . License: Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Dockerfile reference; Docker docs, Running containers. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker docs, Dockerfile reference; Docker docs, Running containers. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Docker WORKDIR and docker run -w." iinventedlight Library, iil:note:n86sdhgd, version of 2026-11-18. https://iinventedlight.com/library/notes/docker-workdir/. License: CC-BY-4.0.
@misc{iil_note_n86sdhgd,
author = {{iinventedlight}},
title = {{Docker WORKDIR and docker run -w}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/docker-workdir/},
note = {Permanent ID iil:note:n86sdhgd. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-workdir.md.