0
0
Dockerdevops~5 mins

WORKDIR instruction for directory in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When building a Docker image, you often need to set the working directory where commands run. The WORKDIR instruction sets this directory inside the container, so you don't have to type full paths every time.
When you want to organize files inside the container in a specific folder.
When you need to run commands like COPY or RUN in a certain directory.
When your application expects to start in a particular folder.
When you want to avoid repeating long directory paths in multiple commands.
When you want to make your Dockerfile cleaner and easier to read.
Config File - Dockerfile
Dockerfile
FROM alpine:3.18
WORKDIR /app
COPY . .
RUN ls -l
CMD ["sh"]

FROM alpine:3.18 sets the base image.

WORKDIR /app sets the working directory inside the container to /app.

COPY . . copies files from your current folder on the host to /app in the container.

RUN ls -l lists files in /app to verify the working directory.

CMD ["sh"] starts a shell when the container runs.

Commands
Builds the Docker image named example-workdir using the Dockerfile in the current directory.
Terminal
docker build -t example-workdir .
Expected OutputExpected
Sending build context to Docker daemon 2.56kB Step 1/5 : FROM alpine:3.18 ---> 3f53bb00af94 Step 2/5 : WORKDIR /app ---> Running in 1a2b3c4d5e6f Removing intermediate container 1a2b3c4d5e6f ---> 7f8e9d0c1b2a Step 3/5 : COPY . . ---> 9a8b7c6d5e4f Step 4/5 : RUN ls -l ---> Running in 0f1e2d3c4b5a total 12 -rw-r--r-- 1 root root 123 Apr 27 12:00 Dockerfile -rw-r--r-- 1 root root 456 Apr 27 12:00 somefile.txt Removing intermediate container 0f1e2d3c4b5a ---> 1a2b3c4d5e6f Step 5/5 : CMD ["sh"] ---> Running in 7e8f9a0b1c2d Removing intermediate container 7e8f9a0b1c2d ---> 3b4c5d6e7f8a Successfully built 3b4c5d6e7f8a Successfully tagged example-workdir:latest
Runs the container and prints the current working directory to confirm WORKDIR is set to /app.
Terminal
docker run --rm -it example-workdir pwd
Expected OutputExpected
/app
--rm - Automatically remove the container after it exits
-it - Run container interactively with a terminal
Runs the container and lists files in the working directory to verify files were copied there.
Terminal
docker run --rm -it example-workdir ls -l
Expected OutputExpected
total 12 -rw-r--r-- 1 root root 123 Apr 27 12:00 Dockerfile -rw-r--r-- 1 root root 456 Apr 27 12:00 somefile.txt
--rm - Automatically remove the container after it exits
-it - Run container interactively with a terminal
Key Concept

If you remember nothing else from this pattern, remember: WORKDIR sets the folder where all following commands run inside the container.

Common Mistakes
Not using WORKDIR and instead typing full paths in every command.
This makes the Dockerfile longer, harder to read, and error-prone if paths change.
Use WORKDIR once to set the directory, then use relative paths in later commands.
Setting WORKDIR to a path that does not exist and expecting it to fail.
Docker automatically creates the directory if it does not exist, so this is not an error but can confuse beginners.
Know that WORKDIR creates the directory if missing, so you can safely set any valid path.
Summary
WORKDIR sets the working directory inside the container for all following commands.
It helps organize files and simplifies command paths in the Dockerfile.
You can verify WORKDIR by running commands like pwd or ls inside the container.