Sub topic 10
Keeping the context small with .dockerignore
This chapter assumes you can run docker build, know that COPY reads files from the build context, and understand that each instruction creates a cached layer. Here you will use a .dockerignore file to control which files enter the context.
Why the context matters
When you run docker build ., the client sends the directory . to the builder before any instruction runs. A node_modules folder with 400 MB of packages or a .git directory with years of history slows every build. The build also becomes less safe, because a stray .env file can end up inside an image through COPY . ..
A .dockerignore file sits at the root of the context. It lists paths that the client leaves out. Excluded files cannot be copied by COPY or ADD, and they do not trigger cache invalidation.
A working example
This file is for the Node.js app from earlier chapters.
node_modules
.git
*.log
.env
Dockerfile
.dockerignoreEach line is a pattern, relative to the context root.
node_moduleskeeps your host's dependencies out. The image gets its own fromnpm ci, and host-built native modules may not match the container's operating system..gitremoves version history.*.logskips log files in the root directory..envkeeps local secrets out of the image.Dockerfileand.dockerignoreare not needed inside the image. The builder still reads them, so excluding them is safe.
Pattern rules
The syntax looks like .gitignore but differs in a few ways.
| Pattern | Meaning |
|---|---|
| `*.log` | Matches `.log` files in the context root only |
| `**/*.log` | Matches `.log` files at any depth |
| `temp?` | Matches `temp` plus one character |
| `!keep.log` | Exception: re-includes a file excluded by an earlier line |
| `# note` | Comment line |
Two details cause most mistakes:
- Patterns are matched against paths relative to the root. A bare
node_modulesdoes not excludepackages/app/node_modules. Use**/node_modulesfor nested copies. - Order matters. The last matching line wins, so put
!exceptions after the pattern they override.
Effect on the build cache
COPY . . computes its cache key from the files it copies. If a log file changes on every run and you do not exclude it, the COPY . . layer and every layer after it rebuild each time. Ignoring volatile files keeps that layer cached when only meaningful sources change.
Allowlist style
For a project with many stray files, you can invert the approach. Exclude everything, then re-include only what the build needs.
*
!package*.json
!server.js
!srcThe * excludes everything at the root. Each ! line brings back one path. This style is strict. A new source file stays out of the image until you add it, which can cause confusing "file not found" errors at runtime.
Common errors
- File not found during COPY. The file is excluded. Check the patterns, especially broad ones like
*. - Nested folders still sent. The pattern lacks
**/. - File is in the wrong place. The file must be named
.dockerignoreand sit at the context root, not next to the Dockerfile in a subdirectory, unless that directory is the context. - Build still slow. Run
docker buildand look at the context transfer line in the output. A large number means something big is still included.
Exercise
Your repository has services/api/node_modules and services/web/node_modules, and the context is the repository root. Your .dockerignore contains node_modules. Do these folders get sent to the builder? What line fixes it?
Show answer
Yes, they are still sent, because node_modules only matches the folder at the context root. Replace the line with **/node_modules to exclude it at any depth.