Docker – WORKDIR Instruction
Last Updated :
19 Dec, 2024
In Docker, organizing and managing file paths within a container is key to building efficient and easy-to-maintain applications. One way to streamline this is by using the WORKDIR
instruction in your Dockerfile. Setting the working directory helps control where commands run, making your Dockerfile more readable and reducing the risk of errors. In this article, we’ll explore how the WORKDIR
instruction works, why it’s helpful, and also discuss alternative ways to manage the working directory, including the use of relative paths and environment variables.
In this article, we will be discussing how to use WORKDIR instruction in your Dockerfile. We will also discuss 2 other ways to issue Working Directory inside your Dockerfile.
WORKDIR Command
WORKDIR instruction is used to set the working directory for all the subsequent Dockerfile instructions. Some frequently used instructions in a Dockerfile are RUN, ADD, CMD, ENTRYPOINT, and COPY. If the WORKDIR is not manually created, it gets created automatically during the processing of the instructions. Some points to be noted when using the WORKDIR instruction are as follows:
- WORKDIR does not create new intermediate Image layers.
- It adds metadata to the Image Config.
- You can have multiple WORKDIR instructions in your Dockerfile.
- If you use relative paths as Working Directory, it will be relative to the previous Working Directory.
- The default path is /
WORKDIR is Helpful in the following ways:
- Simplifies Commands: When you set
WORKDIR
, you no longer have to type out the full path over and over. Commands that follow will automatically run from this set directory, making the Dockerfile cleaner and easier to follow.
- Consistency Across Layers: After setting it, the
WORKDIR
path will stick, so all commands continue to execute in the specified folder unless you change it.
- Fewer Path-Related Errors: With a single
WORKDIR
set, you’re less likely to make mistakes by referencing the wrong path in commands.
For example, setting WORKDIR /app
at the start means you won’t have to keep typing /app
in later commands — they’ll automatically run from that directory.
Work with the WORKDIR Instruction
Follow the below steps to work with the WORKDIR instruction:
Step 1: Create the Dockerfile
You can use the following template to create the Dockerfile.
FROM ubuntu:latest
WORKDIR /my-work-dir
Step 2: Build the Docker Image
To build the Docker Image, you can use the Docker Build command.
sudo docker build -t workdir-demo

Step 3: Run the Docker Container
To run the Docker Container, you can use the Docker Run command.
sudo docker run -it workdir-demo

The above command opens the bash for the container.
Step 4: Verify the Working Directory
You can use the print working directory (pwd) command, to print the working directory.

Now. Let’s discuss ways to issue a working directory in a Dockerfile.
Different Ways to Issue a Working Directory in Dockerfile
There is 2 possible way to do so, and both of them are explained below:
1. WORKDIR by specifying Relative Path
Let’s look at how you can specify a relative path with WORKDIR instruction.
The first step is to create a Dockerfile as mentioned below:
FROM ubuntu:latest
WORKDIR /my-work-dir
RUN echo "work directory 1" > file1.txt
WORKDIR /my-work-dir-2
RUN echo "work directory 2" > file2.txt
Now, build and run the Docker Container.
sudo docker build -t workdir-demo .
sudo docker run -it workdir-demo bash

Finally, print the working directory.
pwd

The first echo statement runs with the working directory set to the “my-work-dir” folder. That’s why if you use ls command to see the contents of the folder, you will find “file1.txt” insidethe “my-work-dir” folder. The second WORKDIR instruction changes the Working Directory to “my-work-dir-2” and hence, the file “file2.txt” is inside that folder.

2. WORKDIR by specifying environment variables
The second way to issue a working directory is by making use of the environment variables. Follow the below steps to take so:
The first step is to use the following Dockerfile template as shown below:
FROM ubuntu:latest
ENV DIRPATH /app
WORKDIR $DIRPATH
Here, we have set the environment variable DIRPATH to the /app directory and set the WORKDIR.
Now, after running the Docker Container, you will find the directory set as /app.
sudo docker build -t workdir-demo .
sudo docker run -it workdir-demo
pwd

WORKDIR vs RUN Command For Directory Management
Both WORKDIR
and RUN
can interact with directories, but they serve different purposes. Here’s a look at how they differ and when you’d use each.
WORKDIR vs RUN mkdir
WORKDIR
: Sets the default directory for all commands that follow, making it the “home base” for those commands. It’s great when you want a specific folder for many commands.
RUN mkdir
: Simply creates a new folder in the container without setting it as the working directory. Use it when you need a new folder but don’t need to move into it.
When to Use Which:
- Use
WORKDIR
if you want to keep a consistent working directory for a series of commands.
- Use
RUN mkdir
to create folders without changing the working directory.
Example:
# Using WORKDIR
FROM python:3.8
WORKDIR /app
RUN touch testfile.txt
Here, testfile.txt
is created in/app
since WORKDIR
is set to /app
.
# Using RUN mkdir
FROM python:3.8
RUN mkdir /app && cd /app && touch testfile.txt
With RUN mkdir
, we create /app
, but it’s not set as the working directory, so every command needs the full path. Understanding when to use WORKDIR
vs. RUN mkdir
will help you avoid path errors and keep your Dockerfile organized and efficient.
Conclusion
In summary, using the WORKDIR
instruction in Docker is an effective way to manage directory paths within your container, reducing repetitive code and keeping your commands organized. By understanding how to set a working directory, whether with absolute paths, relative paths, or environment variables, you gain greater control and flexibility in Dockerfile creation. With this knowledge, you can build Docker images that are clean, efficient, and straightforward to maintain.
Similar Reads
Docker - COPY Instruction
In Docker, there are two ways to copy a file: ADD and COPY. Though there is a slight difference between them regarding the scope of the functions, they more or less perform the same task. In this article, we will primarily focus on the COPY instruction of Docker. So, before discussing the COPY instr
4 min read
Introduction to Docker Swarm Mode
Docker swarm is a container orchestration tool. Swarm Mode in Docker was introduced in version 1.12, enabling the ability to deploy multiple containers on multiple Docker hosts. For this Docker uses an overlay network for the service discovery and with a built-in load balancer for scaling the servic
14 min read
Docker Networking
Pre-requisite: Docker Docker Networking allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help
5 min read
Docker - Installation on Windows
In this article, we are going to see how to install Docker on Windows. On windows if you are not using operating system Windows 10 Pro then you will have to install our docker toolbox and here docker will be running inside a virtual machine and then we will interact with docker with a docker client
2 min read
What Is Docker Network Inspect ?
"docker network inspection" is the command used in the docker CLI to know the detailed information of a particular docker network. Docker network allows you to inspect the configuration and status of a specific Docker network in which you can find the IP address assigned to the docker network the co
5 min read
Docker Tutorial
Docker is a tool that simplifies the process of developing, packaging, and deploying applications. By using containers, Docker allows you to create lightweight, self-contained environments that run consistently on any system, minimizing the time between writing code and deploying it into production.
9 min read
How To Inspect Docker Network?
Applications that use containers require an understanding of Docker's network infrastructure. With an emphasis on security, optimizing, and troubleshooting, this guide delves into Docker network inspection. Learn how to examine networks, comprehend setups, and troubleshoot connectivity problems with
4 min read
Installation of Docker Compose
Docker Compose is a powerful tool that helps in simplifying the orchestration of multi-container docker applications. In this we have to configure the application services inside the Yaml file and docker-compose will read the configuration data from that Yaml script. Before Moving into the installat
4 min read
Docker - Hello World
Have you ever experience that in just one second your entire operating system is ready to use? Yes, you heard it right Docker gives you the facilities to use a new operating system in one second. Docker is a program that uses your base OS resources and only consumes 20MB - 50MB RAM to launch a new O
3 min read
What Is Docker Volume Inspect ?
"docker volume inspect" is a command line interface (CLI) used to extract detailed information about Docker volume. Docker volumes are mainly used to maintain the state of the application which means in other words it is used for the stateful applications. What Is Docker Volume? Docker volume is a w
4 min read