Sub topic 5
Setting up the filesystem: WORKDIR, COPY, and RUN
This chapter assumes you can read a basic Dockerfile, know that docker build sends a build context to the builder, and have a Node.js or similar project to practice with. Three instructions shape the filesystem inside your image: WORKDIR picks a directory, COPY brings files in, and RUN executes commands that change the filesystem.
WORKDIR: a directory that sticks
WORKDIR sets the working directory for every later RUN, COPY, CMD, and ENTRYPOINT in the same stage. If the directory does not exist, Docker creates it.
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=devHere ./ means /app. Use WORKDIR instead of RUN cd /app. A cd inside RUN only lasts for that one command, because each RUN starts a fresh shell. WORKDIR persists, and relative paths after it resolve against it.
COPY: from the context into the image
COPY <src> <dest> copies files from the build context into the image. Three rules cause most mistakes:
- Source paths are relative to the context root, not to the Dockerfile. You cannot copy
../secrets.txt, because it lies outside the context. The build fails with a "forbidden path outside the build context" error. - When the source is a directory, Docker copies its contents, not the directory itself.
COPY src/ /app/puts the files ofsrcdirectly into/app. - Relative destinations resolve against
WORKDIR. With several sources, end the destination with/.
Files copied by COPY are owned by root unless you say otherwise. Use --chown when a non-root user must write to them:
COPY --chown=node:node . .ADD looks similar but also fetches URLs and unpacks local tar archives. That extra behavior is easy to trigger by accident, so prefer COPY unless you need it.
RUN: commands that build layers
RUN executes a command at build time and saves the resulting filesystem changes as a new layer. Its output stays in the image, but the process does not keep running.
A common pattern is installing system packages:
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*Chaining with && keeps the install and the cleanup in one layer. If you ran rm in a separate RUN, the package lists would still exist in the earlier layer, and the image would not shrink. Running apt-get update in its own RUN is also risky, because a cached update layer can go stale while the install step reruns.
Ordering for the cache
Look again at the order in the diagram. The manifests are copied first and the dependencies installed, and only then is the rest of the source copied. Editing server.js changes only the last layer, so Docker reuses the cached npm ci result. If you wrote COPY . . before RUN npm ci, every source edit would invalidate the install layer and reinstall everything.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `COPY failed: file not found in build context` | Path is wrong, or `.dockerignore` excludes the file | Check the path relative to the context root and your ignore rules |
| Files land in the wrong place | A directory source copies its contents | Name the destination directory explicitly |
| `cd` has no effect on the next step | Each `RUN` starts a new shell | Use `WORKDIR` |
| Image stays large after cleanup | Files were deleted in a later layer | Delete in the same `RUN` that created them |
| `EACCES` permission errors at run time | Files are owned by root but the app runs as another user | Use `COPY --chown` |
Exercise
A Dockerfile has COPY . . followed by RUN npm ci. Builds are slow because dependencies reinstall after every code change. How do you fix it, and why does the fix work?
Show answer
Copy only package*.json first, run npm ci, and then run COPY . .. The install layer's cache key depends only on the manifest files and the earlier layers. A source edit changes only the final COPY layer, so Docker reuses the cached install layer.