Docker – WORKDIR Instruction
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
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. 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.
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.
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
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.