0
0
Dockerdevops~10 mins

WORKDIR instruction for directory in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - WORKDIR instruction for directory
Start Dockerfile
Read WORKDIR instruction
Check if directory exists
Yes No
Set as current
Use directory for next commands
Continue build
Docker reads the WORKDIR line, checks if the directory exists, creates it if needed, then sets it as the working directory for following commands.
Execution Sample
Docker
FROM alpine
WORKDIR /app
RUN touch file.txt
Sets /app as working directory, creates it if missing, then runs command inside /app.
Process Table
StepInstructionActionDirectory StateCurrent Working Directory
1FROM alpineStart from alpine base image//
2WORKDIR /appCheck /app exists? No, create /app/app created/app
3RUN touch file.txtRun command inside /app/app exists with file.txt/app
4EndBuild continues with /app as working dir/app exists with file.txt/app
💡 WORKDIR sets /app as current directory; created if missing; subsequent commands run there.
Status Tracker
VariableStartAfter Step 2After Step 3Final
Current Directory//app/app/app
Directory /app ExistsNoYesYesYes
Files in /appNoneNonefile.txtfile.txt
Key Moments - 2 Insights
Why does Docker create the directory if it does not exist when using WORKDIR?
Docker ensures the directory exists so commands run inside it without errors, as shown in execution_table step 2 where /app is created.
Does WORKDIR change the directory for the whole build or just the next command?
WORKDIR sets the directory for all following commands, not just the next one, as seen in steps 3 and 4 where current directory remains /app.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current working directory after the WORKDIR instruction?
A/app
B/
C/root
D/home
💡 Hint
Check the 'Current Working Directory' column at step 2 in the execution_table.
At which step does Docker create the /app directory?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Directory State' column in execution_table for when /app is created.
If the WORKDIR was set to an existing directory, what would change in the execution table?
ADocker would create a new directory anyway
BDocker would delete the directory first
CDocker would skip creating the directory
DDocker would fail the build
💡 Hint
Refer to the decision in concept_flow where directory existence affects creation.
Concept Snapshot
WORKDIR /path
- Sets working directory for following commands
- Creates directory if missing
- Affects all subsequent RUN, CMD, ENTRYPOINT
- Simplifies file paths in Dockerfile
- Helps organize container filesystem
Full Transcript
The WORKDIR instruction in Dockerfile sets the working directory for all following commands. When Docker reads WORKDIR, it checks if the directory exists inside the image. If it does not exist, Docker creates it automatically. Then Docker uses this directory as the current working directory for subsequent commands like RUN or CMD. This means commands run inside that directory without needing full paths. For example, WORKDIR /app creates /app if missing and sets it as current directory. Then RUN touch file.txt creates file.txt inside /app. This behavior helps organize files and commands inside containers cleanly.