Sub topic 8
Keeping the build context small with .dockerignore
This chapter assumes you can run docker build, read a simple Dockerfile, and know that a COPY step creates a layer. It covers what the build context is, how it affects the cache, and how .dockerignore keeps it small.
What the build context is
When you run docker build ., the trailing . is the build context. The client sends the contents of that directory to the builder. Every COPY and ADD can only read files from this set.
The context is sent before any instruction runs. A large context slows every build, even one that is fully cached. If a node_modules directory or a .git history is in the context, it is transferred each time unless you exclude it.
How the context affects the cache
For COPY and ADD, the cache check compares the content of the files being copied, not only the instruction text. If any copied file changes, the step is a cache miss, and every later step rebuilds as well.
This matters most for COPY . .. It copies the whole context, so a change to any file in it invalidates that layer. Files that are irrelevant to the build can then trigger rebuilds. Typical examples are:
- Log files written during local development.
- Editor swap files and
.DS_Store. - The
.gitdirectory, which changes with every commit. - Local
node_modules, which can also overwrite the dependencies you installed in the image.
Excluding these files makes COPY . . a cache hit when only irrelevant files changed.
Writing a .dockerignore file
Place a file named .dockerignore in the root of the build context. Each line is a pattern, similar to .gitignore. Lines starting with # are comments.
# Version control
.git
# Dependencies installed inside the image
node_modules
# Build output and local artifacts
dist
*.log
.DS_Store
# Secrets and local configuration
.envPatterns are matched relative to the context root. A few rules are worth knowing:
| Pattern | Meaning |
|---|---|
| `node_modules` | Excludes the `node_modules` entry in the context root. |
| `**/node_modules` | Excludes `node_modules` at any depth. |
| `*.log` | Excludes matching files in the context root only. |
| `**/*.log` | Excludes matching files at any depth. |
| `!keep.log` | Re-includes a file that an earlier pattern excluded. |
An allowlist approach
For projects with many stray files, you can exclude everything and re-include only what the build needs. Later lines override earlier ones.
*
!package.json
!package-lock.json
!src
!tsconfig.jsonNew files are then excluded by default, so the context cannot grow by accident.
Seeing the effect
Build the same project before and after adding the file. BuildKit prints the amount of data it transfers in the first lines of output.
docker build --progress=plain -t myapp:dev .Look for a line like transferring context: 245.31MB. After adding .dockerignore, the number should drop to the size of your actual sources.
Common mistakes
- Placing
.dockerignoreoutside the context root. Docker only reads it from the root of the context you pass to the build. - Ignoring a file that a
COPYinstruction needs. The build then fails with a "not found" error. - Excluding
Dockerfileitself. Docker still reads it, but the file will not be available toCOPY, which is usually fine. - Assuming
.dockerignoreprotects secrets in the image. It only keeps files out of the context. Anything you copy in still ends up in a layer.
Exercise
Your build context is 400 MB because of a local node_modules directory, and COPY . . re-runs whenever you edit a log file in logs/app.log. Which two lines in .dockerignore fix both problems, and why does the second one help the cache?
Show answer
Add node_modules and **/*.log (or logs). The first removes the 400 MB directory from the context, so it is no longer transferred. The second means edits to log files no longer change the content that COPY . . reads, so that step stays a cache hit and later steps are not invalidated.