.dockerignore Explainer · Guide

Why a .dockerignore pattern does not match in subfolders

Most .dockerignore files start life as a copy of a .gitignore, and they look alike. They are read by different code with different rules. Each example below shows what BuildKit, the builder behind docker build since Docker Engine 23, actually sends. Every one was run through BuildKit's own matching code (moby/patternmatcher v0.6.1 and tonistiigi/fsutil), not guessed.

1. Patterns start at the context root

*.log
PathSent?
a.logno
logs/b.logyes

Docker matches each pattern against the whole path, from the top of the context. * never crosses a /, so *.log matches only names at the top. Git matches a slash-free pattern at every depth, which is why the same line works there. The fix is **/*.log. The same applies to node_modules in a monorepo: packages/web/node_modules is sent unless you write **/node_modules.

2. !folder does not reach files matched directly

**/*.md
!docs
PathSent?
x.mdno
docs/README.mdno
docs/a.txtyes

BuildKit walks the folders and remembers, for each line, whether it matched a folder above the current path. An exception line inherited from a parent counts only if the line was actually checked at the parent. It is checked there only when the parent was excluded at that point. docs itself was never excluded, so !docs was skipped there. **/*.md then matches docs/README.md directly and nothing undoes it. Write !docs/**, which matches the files themselves, and all three answers come out as you meant.

The reverse case works: * followed by !src sends everything under src/, because src was excluded by * first.

3. [!a] is not a negation, and [^a] matches /

[!a]

This excludes files named ! and a, and keeps b. Go negates a class with ^. Docker also turns each pattern into a regular expression. There a negated class is any character but the listed ones, slashes included, so a[^x]b excludes a/b and everything inside it.

4. Comments only in the first column

  # not a comment
node_modules # deps

Both lines are patterns. The first is trimmed to # not a comment and matches a file of that name. The second looks for a path literally called node_modules # deps, so node_modules/ is sent in full. A comment is a line whose first character is #.

5. One bad line fails every build

node_modules
!node_modules/keep
[

An unclosed [ or a lone ! makes BuildKit refuse the whole file (invalid excludepatterns: syntax error in pattern). Some lines pass the first syntax check and fail only when a path reaches them, for example a backslash before most letters, which Go's regular expressions reject.

Check your own file

The .dockerignore explainer runs the same code in your browser. Give it your .dockerignore, a file list and your Dockerfile. It names the line behind every path, flags COPY sources that would fail and secrets still sent, and writes a fix it re-checks against your files.

Questions

Why doesn't *.log in .dockerignore match logs/app.log?

A .dockerignore pattern is matched against the whole path from the root of the build context. *.log can only match a name with no slash in it, so it matches app.log at the top and nothing deeper. Write **/*.log. The same goes for node_modules, __pycache__ and .DS_Store: use **/node_modules and so on.

Why does a pattern with a slash work differently in .gitignore?

Git anchors a pattern to its folder only when it contains a slash, and otherwise matches it at any depth. Docker anchors every pattern to the context root. So the one-word patterns copied from a .gitignore are the ones that silently stop working.

How do I re-include one folder after excluding a file type everywhere?

Write the exception for the folder's contents, as in **/*.md followed by !docs/**. A bare !docs does nothing for files that the earlier line matched directly, because BuildKit carries a folder's exception down only when the folder itself was excluded.

Why is [!a] not working in my .dockerignore?

Docker's patterns use Go's syntax, where a class is negated with ^. [!a] means the character ! or a. Use [^a], and remember that in Docker a negated class can match a slash too.