Sub topic 6
Filtering the Context with .dockerignore
This chapter assumes you can write a simple Dockerfile, run docker build and docker run, and know that the build context is the directory you pass as the last argument to docker build. Here you learn how a .dockerignore file removes files from that context before the builder sees them.
Why filter the context
Docker sends the whole build context to the builder, so every file in it counts, even a file no COPY instruction mentions. A .git directory, a node_modules folder, and local .env files are typical examples. Left in the context, they slow the transfer, they can invalidate cached layers, and they can end up in the image through COPY . ..
A .dockerignore file lists patterns that the builder removes from the context before the build starts. Excluded files cannot be used by COPY or ADD.
Where the file goes
Docker looks for a file named .dockerignore at the root of the build context. A .dockerignore file in a subdirectory of the context is not read.
A typical file
Each line is one pattern, matched against paths relative to the context root. Lines that start with # are comments.
# Version control and local tooling
.git
.gitignore
# Dependencies are installed inside the image
node_modules
# Logs at any depth
**/*.log
# Local secrets
.env
.env.*
# Docs are not needed in the image, except the README
*.md
!README.mdHere is how the patterns behave:
.gitandnode_modulesexclude those entries at the context root.**/*.logmatches.logfiles at any depth, including the root..env.*matches files such as.env.local.*.mdexcludes Markdown files in the context root. The!README.mdline then putsREADME.mdback.
A pattern like *.md only matches in the root. A pattern like */*.md matches one directory down, and **/*.md matches at every depth.
Exceptions and order
A line that starts with ! is an exception. It re-includes files that an earlier pattern excluded. The last matching rule decides the result, so order matters.
*.md
!README*.md
README-secret.mdThis excludes all Markdown files in the context root. It then re-includes every file matching README*.md, and finally excludes README-secret.md again. If you moved the last line above the ! line, README-secret.md would be included, because the exception would come last.
What the filter does not do
The filter only changes what the builder receives. It does not change your COPY lines. If a COPY source is excluded, the build fails with a "not found" error, which is a common surprise. For example, if .dockerignore contains *.md and your Dockerfile runs COPY docs.md /app/, the build cannot find docs.md.
Checking the result
To see which files survive filtering, copy the whole context into a throwaway image and list it. Save this as Dockerfile.debug:
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD ["find", "."]Then build and run it:
docker build -t context-debug -f Dockerfile.debug .
docker run --rm context-debugThe output lists every file the build received. If a file you expected is missing, check the patterns and their order. If a file you meant to exclude is present, check that the pattern matches the path from the context root.
Common errors
| Symptom | Likely cause |
|---|---|
| `COPY` reports a file not found | The file matches a `.dockerignore` pattern |
| `.dockerignore` seems ignored | The file is not at the context root, or the name is misspelled |
| A pattern does not match nested files | `*.log` only matches the root. Use `**/*.log` |
| An exception has no effect | A later rule excludes the file again |
Exercise
A .dockerignore contains the lines !config.json and then *.json. Your Dockerfile runs COPY config.json /app/, and the build fails. Why, and how do you fix it?
Show answer
The last matching rule decides. *.json comes after !config.json, so it excludes config.json again. Swap the order so *.json comes first and !config.json second.