Open In App

Docker – WORKDIR Instruction

Last Updated : 28 Oct, 2020
Like Article

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 /

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. 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

building docker image

Step 3: Run the Docker Container

To run the Docker Container, you can use the Docker Run command.

sudo docker run -it workdir-demo

running the container

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.

verifying the directory

Now. Let’s discuss ways to issue a working directory in a 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

issuing working directory

Finally, print the working directory.


verifying the directory

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” inside the “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.

listing directory

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

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

issuing working directory

To conclude, in this article, we discussed how to use the WORKDIR instruction in the Dockerfile to set the working directory. We also saw how to set a working directory using relative paths and environment variables.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads