Sub topic 4
Trimming the build context with .dockerignore
This chapter assumes you can read a Dockerfile, run docker build and docker run, and know that COPY adds files to an image layer. It covers how .dockerignore shrinks the build context and protects the build cache.
What the build context is
When you run docker build ., the final . is the build context. Docker sends the files in that directory to the builder before it runs any instruction. Every COPY and ADD can read only from this context.
Sending a large context costs time even if your Dockerfile never uses most of it. A local node_modules folder or a .git history can add hundreds of megabytes to every build.
What goes wrong without a .dockerignore
Three problems appear when the context holds files it should not:
- Slow builds. The builder transfers everything first. This is most noticeable when the builder runs on a remote machine.
- Bloated images.
COPY . .copies everything in the context into a layer, including logs and local dependencies. - Leaked files. An
.envfile with credentials is copied into the image, and anyone who can pull the image can read it.
There is a fourth problem, which affects the cache. A COPY . . instruction is cached by a checksum of the files it copies. If a log file or .git data changes on every commit, the checksum changes and Docker rebuilds that layer and every layer after it. Excluding those files keeps the layer cached.
Writing the file
Put a file named .dockerignore in the root of the build context, next to the Dockerfile. Each line is a pattern, relative to the context root.
.git
node_modules
*.log
.env
Dockerfile
.dockerignore.gitkeeps version control data out of the image.node_moduleskeeps your local dependencies out. The image should install its own withnpm ci, because your local copy may be built for a different operating system.*.logexcludes log files in the root of the context..envkeeps local secrets out.Dockerfileand.dockerignoreare not needed inside the image. Docker still reads them to run the build, so excluding them from the copy is safe.
Pattern rules to know
The syntax looks like .gitignore, but it differs in a few ways.
| Pattern | Meaning |
|---|---|
| `*.log` | Matches `.log` files in the context root only |
| `**/*.log` | Matches `.log` files at any depth |
| `tmp/` | Matches the `tmp` entry in the root |
| `!keep.log` | Exception: includes a file that an earlier pattern excluded |
Two details cause most errors:
- Patterns are matched from the context root, not from the directory where the file lives.
node_modulesalone does not excludeweb/node_modules. Use**/node_modulesfor nested projects. - Order matters for
!exceptions. The last matching pattern wins, so put the exception after the pattern it overrides.
Seeing the effect
The build output reports how much data was sent. With BuildKit, look for the transferring context line.
docker build -t myapp:small .Run this before and after adding .dockerignore. The line reads something like transferring context: 250MB before and a few kilobytes after. The exact numbers depend on your project.
You can also use the pattern to deliberately allow only what you need, by excluding everything and adding exceptions:
*
!package*.json
!srcThis allow-list style is stricter. New files are excluded by default, so you have to opt them in. The trade-off is that a build that suddenly fails with "file not found" on COPY usually means you forgot an exception.
Common errors
- A
COPYfails with "not found". The file is excluded by a pattern. Check for a broad rule such as*or a directory name that also matches the file you need. - The ignore file seems to do nothing. It must be in the context root, and the name must be exactly
.dockerignore. If you pass a different Dockerfile with-f, Docker looks for<Dockerfile name>.dockerignorenext to it first, with BuildKit. - Nested dependencies still get copied. Use
**/node_modulesinstead ofnode_modules.
Exercise
Your repository has api/ and web/ projects, each with its own node_modules. You build from the repository root with .dockerignore containing only node_modules, and the context is still large. Why, and what is the fix?
Show answer
Patterns match from the context root, so node_modules excludes only the root-level folder, not api/node_modules or web/node_modules. Change the pattern to **/node_modules to match it at any depth.