Sub topic 7
Writing .dockerignore patterns
This chapter assumes you can write a basic Dockerfile and run docker build. You should also know that the build context is the set of files sent to the builder, and that .dockerignore filters that set before the build starts.
Where the file lives and how it is read
Docker reads .dockerignore from the root of the build context. Each line is one pattern. Patterns are matched against file paths relative to that root, using Go's filepath.Match rules with a few additions. A path that matches is removed from the context, so COPY and ADD cannot see it.
Docker also removes leading and trailing whitespace from each line. It also ignores blank lines.
Comments
A line whose first character is # is a comment. The # must be the first character. A # later in a line is part of the pattern.
# This line is a comment
node_modulesWildcards
Patterns use these wildcards:
| Pattern | Meaning |
|---|---|
| `*` | Matches any sequence of characters, except the path separator `/`. |
| `?` | Matches exactly one character, except `/`. |
| `**` | Matches any number of directories, including none. |
| `[abc]` | Matches one character from a set or range, such as `[a-c]`. |
Because * does not cross /, a pattern like *.log only matches .log files in the context root. It does not match logs/app.log. Use **/*.log to match at any depth.
Patterns are anchored to the root
A pattern is matched from the context root, not from any directory. The pattern temp excludes temp in the root. It does not exclude src/temp. To match at any depth, write **/temp.
Docker also cleans each pattern before matching. A leading / is dropped, and . and .. elements are resolved. So /temp and temp mean the same thing.
When a pattern matches a directory, everything inside is excluded too. You do not need node_modules/**. Plain node_modules is enough.
A typical file
# 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.mdThe last two lines use an exception, which starts with !. The next chapter covers exceptions in detail. For now, note that !README.md puts back a file that *.md removed.
How the rules are evaluated
The diagram shows how Docker decides about a single path.
The last matching line wins. This is why order matters, as the next chapter shows.
Common mistakes
- Expecting
*.logto match at any depth. It matches only in the root. Use**/*.log. - Ignoring a file the Dockerfile needs. If
COPY package.json ./fails with "not found", check that.dockerignoredoes not exclude it. - Putting
.dockerignorein the wrong place. It must sit at the context root. A file in a subdirectory has no effect, unless that subdirectory is the context. - Adding a trailing comment.
node_modules # depsis read as one pattern with spaces and a#in it.
Check your patterns
To confirm what a pattern removes, list the files that reach the build. A later chapter shows a debug image for this.
Exercise
Your .dockerignore contains *.log. A file logs/app.log still ends up in the image. Why, and what is the fix?
Show answer
* does not match the / separator, so *.log only matches .log files in the context root. Replace it with **/*.log to match at any depth.
Exercise
Which paths does the pattern temp exclude: temp, temp/cache.db, src/temp?
Show answer
temp and temp/cache.db. Matching a directory excludes its contents. src/temp stays because patterns are matched from the context root. Use **/temp to exclude it as well.