Sub topic 9
Context size and layer cache behavior
This chapter assumes you can write a Dockerfile with FROM, COPY and RUN. It also assumes you know that docker build takes a context path and that .dockerignore filters that context. Here you will see how the files in the context affect build time and cache hits.
Why context size matters
Before the first instruction runs, the builder needs the files from your context. Large directories such as node_modules, .git, build output and logs slow this step down.
- The legacy builder uploads the whole context to the daemon on every build.
- BuildKit transfers only the files the build needs and only the parts that changed since last time. Its first
transferring contextline still grows with large, unfiltered directories.
The size is easy to see in the build output:
docker build -t myapp .
# BuildKit prints a line like:
# => [internal] load build context
# => => transferring context: 412.31MBIf that number is much larger than your source code, something is in the context that should not be.
How the cache decides
Docker caches each instruction as a layer. For RUN, the cache key is mostly the instruction text and the parent layer. For COPY and ADD, the builder also checks the files being copied. If their contents or metadata differ from the cached layer, the cache misses. Modification times are ignored.
A cache miss invalidates every layer after it. A COPY . . copies the entire context, so any change to any file in the context rebuilds that layer and everything below it.
Order instructions by how often they change
Copy the files that change rarely first, and the files that change often last:
FROM node:20-slim
WORKDIR /app
# Changes only when dependencies change
COPY package.json package-lock.json ./
RUN npm ci
# Changes on almost every commit
COPY . .
CMD ["node", "server.js"]Editing server.js now reruns only the last COPY. The slow npm ci layer stays cached.
Where .dockerignore helps the cache
Files excluded by .dockerignore are not in the context. That means they cannot be copied and cannot change a COPY checksum. Typical sources of unwanted cache misses:
| Excluded path | Problem it prevents |
|---|---|
| `.git` | Every commit, fetch or checkout changes files here, so `COPY . .` misses even when your source is identical. |
| `node_modules` | Host-installed dependencies are copied over the ones `npm ci` installed, and they differ per machine. |
| `**/*.log` | Logs grow while you work, so the checksum changes on every build. |
| `.env`, `.env.*` | Local settings differ per developer and should not be in the image anyway. |
.git
node_modules
**/*.log
.env
.env.*The same rule keeps the context small, so the transfer step is faster too.
Common mistakes
- Putting
COPY . .before the dependency install, which rerunsnpm cion every source edit. - Excluding files the build needs, for example ignoring
package-lock.json. The build then fails or silently uses different versions. - Expecting
.dockerignoreto stop a file from affecting aCOPYof a directory when the file is still inside it. Only excluded files are removed, so check the rest.
Exercise
A Dockerfile ends with COPY . .. Each commit makes the build rerun that layer, even when you only changed a README that the image does not use. Name two fixes.
Show answer
Add the file to .dockerignore (for example *.md), so it is no longer in the context and cannot change the COPY checksum. Also exclude .git, which changes on every commit. Alternatively, copy only the directories the image needs, such as COPY src/ ./src/, so unrelated files are never part of that layer.