1. Overview

In this tutorial, we’ll learn about the WORKDIR instruction in a Dockerfile. We’ll discuss what it does, the default value, and how to use it in a Dockerfile. We’ll also discuss its importance in Dockerfile creation.

2. What Is the WORKDIR Instruction

The WORKDIR instruction in a Dockerfile sets the current working directory for subsequent instructions in the Dockerfile. When the WORKDIR instruction is executed, all the subsequent instructions in the Dockerfile will be executed relative to the specified directory.

For example, if the WORKDIR instruction is set to /app, then all the subsequent instructions will be relative to the /app directory. The WORKDIR instruction switches to a specific directory in the Docker image, like the application code directory, to make it easier to reference files in subsequent instructions.

It is important to note that the WORKDIR instruction only sets the current working directory for subsequent instructions in the Dockerfile. If the WORKDIR instruction uses the default value (/) of the root directory, but there are no following instructions in the Dockerfile, it’ll not have any effect on the current working directory.

3. The Default WORKDIR

If the WORKDIR instruction is not specified in a Dockerfile, the default WORKDIR is the root directory (/). In other words, if there is no WORKDIR instruction in a Dockerfile, then all the subsequent instructions will execute relative to the root directory. However, we can override this default value by specifying a different directory in the WORKDIR instruction.

Note that the default value of the WORKDIR instruction is always the root directory (/), regardless of the Docker image.

The default value of the WORKDIR instruction can affect the behavior of commands that rely on the working directory. For example, there is a command in the Dockerfile that uses the cd command to change to a specific directory. Now, if we don’t specify the WORKDIR instruction, then the Dockerfile will use the default value “/“. Now, the cd command will try to change to that directory in the root directory of the filesystem in the Dockerfile. The command will fail if the directory doesn’t exist in the root directory.

4. Changing the WORKDIR

In Docker, we can also change the working directory of a container. We can use the WORKDIR instruction to specify the working directory for the command specified in the CMD/ENTRYPOINT instructions. Let’s look into using the WORKDIR in a Dockerfile:

FROM centos 
WORKDIR /home
ENTRYPOINT ["sh", "-c", "pwd"]

In this case, the WORKDIR instruction sets the working directory to /home. The ENTRYPOINT instruction specifies the pwd command to execute in the /home directory of the container.

Alternatively, we can also specify the working directory when running a Docker container using the –w or –workdir option:

$ docker run -w /home centos:latest sh -c "pwd"

This Docker run command will execute the pwd command in the /home directory inside the container.

5. Conclusion

In this article, we discovered the default value of the WORKDIR instruction is “/“. A WORKDIR instruction switches between different directories in the Docker image. We can override it by specifying a custom value for the WORKDIR instruction.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments