Menu
Coddy logo textTech

The WORKDIR Instruction

Part of the Introduction to Docker section of Coddy's Terminal journey — lesson 26 of 40.

The WORKDIR instruction sets the working directory for the instructions that follow it. It is the Dockerfile equivalent of cd, and it keeps your image tidy by putting your files in one known place instead of scattering them at the root:

FROM alpine
WORKDIR /app
COPY data.txt data.txt

Because WORKDIR /app runs first, the relative COPY destination is taken from inside /app, so the file lands at /app/data.txt. You can read it back from that full path:

docker build -t app .
docker run app cat /app/data.txt
challenge icon

Challenge

Medium

A file named info.txt is already in your directory. Write a Dockerfile that is FROM alpine, sets WORKDIR /project, then copies info.txt to /project/info.txt. Build it tagged located, then run it to cat /project/info.txt.

Cheat sheet

WORKDIR sets the working directory for subsequent instructions (equivalent to cd). Relative paths in instructions like COPY resolve from this directory:

FROM alpine
WORKDIR /app
COPY data.txt data.txt  # lands at /app/data.txt

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction to Docker