Docker can automatically create images by following the instructions in a Dockerfile. A Dockerfile is a text file that includes all the commands a user might use in the command line to build an image. Now, the Docker WORKDIR instruction sets the working directory for all following Dockerfile instructions. Common instructions in a Dockerfile include RUN, ADD, CMD, ENTRYPOINT, and COPY. If the WORKDIR is not created manually, it will be created automatically when processing the instructions. Here are some important points to consider when using the WORKDIR instruction:
Today, you’ll find straightforward solutions for efficient container setup, and for that, I’ll teach you how to use the Docker WORKDIR command.
The Dockerfile supports the following instructions:
Instruction | Description |
---|---|
ADD | Add local or remote files and directories. |
ARG | Use build-time variables. |
CMD | Specify default commands. |
COPY | Copy files and directories. |
ENTRYPOINT | Specify default executable. |
ENV | Set environment variables. |
EXPOSE | Describe which ports your application is listening on. |
FROM | Create a new build stage from a base image. |
HEALTHCHECK | Check a container’s health on startup. |
LABEL | Add metadata to an image. |
MAINTAINER | Specify the author of an image. |
ONBUILD | Specify instructions for when the image is used in a build. |
RUN | Execute build commands. |
SHELL | Set the default shell of an image. |
STOPSIGNAL | Specify the system call signal for exiting a container. |
USER | Set user and group ID. |
VOLUME | Create volume mounts. |
WORKDIR | Change working directory. |
How WORKDIR Improves Dockerfile Efficiency
The Docker WORKDIR instruction defines the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD commands that follow it in the Dockerfile. If the WORKDIR does not exist, it will be created even if it is not used in any later Dockerfile command.
The WORKDIR instruction can be applied multiple times in a Dockerfile. If a relative path is given, it will be relative to the previous WORKDIR path. For instance:
WORKDIR /a<br>WORKDIR b<br>WORKDIR c<br>RUN pwd
The result of the last pwd command in this Dockerfile would be /a/b/c.
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
The WORKDIR instruction can utilize environment variables that were set earlier using ENV. You can only use environment variables that are explicitly defined in the Dockerfile. For example:
ENV DIRPATH=/path<br>WORKDIR $DIRPATH/$DIRNAME<br>RUN pwd
The result of the last pwd command in this Dockerfile would be /path/$DIRNAME.
If not defined, the default working directory is /. In practice, if you are not building a Dockerfile from scratch (FROM scratch), the WORKDIR is likely set by the base image you are using.
Thus, to prevent unintended actions in unknown directories, it is advisable to set your WORKDIR clearly.
Docker WORKDIR Syntax (With Examples)
The basic Docker Workdir syntax is:
WORKDIR /path/to/directory
Examples
Poor: this presumes that WORKDIR in the base image is / (if that changes upstream, the web stage will fail).
FROM nginx AS web<br>WORKDIR usr/share/nginx/html<br>COPY public .
Excellent: a leading slash guarantees that WORKDIR consistently points to the correct path.
FROM nginx AS web<br>WORKDIR /usr/share/nginx/html<br>COPY public .
This Results in:
PS E:\myproject> docker build -t cmd .
Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM microsoft/nanoserver
---> 22738ff49c6d
Step 2/2 : COPY testfile.txt c:\RUN dir c:
GetFileAttributesEx c:RUN: The system cannot find the file specified.
PS E:\myproject>
Reusing WORKDIR
This Docker WORKDIR command can be used again to define a new working directory at any point in the Dockerfile. The path to the new working directory should be specified relative to the existing working directory.

FROM ubuntu:16.04 WORKDIR /project RUN npm install WORKDIR ../project2 RUN touch file1.cpp
Although you can create and change directories manually, it is highly advised to use Docker WORKDIR to indicate the current directory you want to work in, as it simplifies troubleshooting.
Real-World Examples of Using WORKDIR in Dockerfiles
Node.js Example
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
Go Example
FROM golang:1.20
WORKDIR /go/src/app
COPY . .
RUN go build -o main .
CMD ["./main"]
Let’s Wrap Up!
The Docker WORKDIR command is straightforward yet effective in Docker. It helps keep your Dockerfiles tidy, minimizes mistakes, and simplifies container maintenance. When you establish an appropriate working directory, you create a more efficient, polished, and production-ready container configuration.
FAQ’s
1. Is Docker WORKDIR required in a Dockerfile?
No, WORKDIR is not mandatory. If you don’t set it, Docker will default to / (root). But using WORKDIR makes Dockerfiles cleaner and avoids repetitive cd commands.
2. Why should I use WORKDIR if Docker already knows where files go?
Docker doesn’t “guess” where to run commands. WORKDIR defines the default directory inside the container, so commands like RUN, COPY, or CMD execute in the right place. Without it, you risk misplaced files or build errors.
3. Does WORKDIR affect all users inside a container?
Yes. WORKDIR sets the starting directory for processes inside the container, no matter who runs them. You can change the user with the USER instruction, but the default working directory will remain as defined by WORKDIR Docker unless overridden.
4. Can I override Docker WORKDIR when running a container?
Yes. You can override it with commands like:
docker run -w /custom/path myimage
This temporarily changes the working directory without modifying the Dockerfile.
5. What happens if I set multiple WORKDIR instructions?
Each WORKDIR updates the current path. If you chain them, they build on each other. Example:
WORKDIR /app<br>WORKDIR logs
Final path = /app/logs.