Open In App

Docker – WORKDIR Instruction

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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.

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

issuing working directory

Finally, print the working directory.

pwd

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

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

issuing working directory

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.



Next Article
Article Tags :

Similar Reads