Working directory
- Docker & Kubernetes course
- Beginner
- Lesson 34/82
- 4 min
Learn to set the working directory with -w.
Working directory
-w (--workdir) sets the starting directory for the process inside the container; it overrides WORKDIR from the Dockerfile. It is widely used to mount a folder and run a command there:
docker run --rm -w /tmp alpine pwd
# /tmp
docker run --rm -v "$PWD":/src -w /src node:20 npm installThe second command runs npm install in the project folder on the host without installing Node.
Overriding image settings
docker run can override most of the settings in the image: the command (arguments after the image), the working directory (-w), the entry point (--entrypoint), the user (-u) and environment variables (-e). Replacing the entrypoint with a shell to look inside an image is common:
docker run --rm -it --entrypoint sh api:1.0Practice Run a tool in the project
Write the command for a container, deleted when done, that mounts the current folder ("$PWD") at /src in the container, sets the working directory to /src and runs npm test in the node:20 image.
Starter code
# buraya yazExpected output
> api@1.0.0 test > node --test # pass 12 # fail 0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 With WORKDIR /app in the Dockerfile, what does docker run -w /data img pwd print?
- /
- It errors
- /data Correct answer
- /app
Explanation: Run-time options override image settings.
2 Replace the image's entry point with a shell.
docker run --rm -it ____ sh api:1.0
docker run --rm -it --entrypoint sh api:1.0
Explanation: --entrypoint overrides the ENTRYPOINT instruction.
3 Match.
| Term | Meaning |
|---|---|
-w |
Working directory |
--entrypoint |
Entry point |
-u |
User |
arguments after the image |
Command |