.dockerignore Cheatsheet - Docker Build Context Ignore Rules

If you skip .dockerignore, docker build . packs node_modules and .git into the build context, slowing builds and bloating images. This reference covers 5 groups: basic syntax, glob patterns, common ignores, differences from .gitignore, and slimming tips. After writing a template, verify the context size with docker build, and remember a ! negation only re-includes items already ignored. After reading you can ship a safe, lean .dockerignore for your project.

Config Formats·26 commands·Last updated 2026-08-02
.dockerignoredockerBuild ContextconfigIgnore

Basic Syntax 6

node_modules
Ignore a file or dir named node_modules
*.log
Ignore all .log files
tmp/
Only ignore tmp at root (with slash)
**/tmp
Ignore tmp at any level
# comment
Hash starts a comment line
Empty lines are ignored

Glob Patterns 6

*
Match any number of non-separator chars
?
Match a single non-separator char
**
Match any directory level
[abc]
Match any one char in brackets
*.tmp
Ignore all .tmp files
dist/*
Ignore dist's direct children (keep dist itself)

Common Ignores 6

.git
Don't bake the git repo into the build context
node_modules
Dependency dir, usually installed inside the image
.env
Don't leak secrets into the build env
Dockerfile
Ignore itself (rarely needed)
*.md
Ignore docs
.dockerignore
Ignoring itself doesn't affect the current build

vs .gitignore 4

!node_modules/important
Use ! to negate and re-include an ignored item
Dockerfile ignores .gitignore
Dockerfile ignores .gitignore
Build context = docker build .
Build context = docker build .
COPY is still path-limited
COPY is still path-limited

Slimming Tips 4

**/*.pyc
Ignore all compiled caches
.cache/
Ignore local cache dir
coverage/
Ignore coverage reports
tests/
Ignore test dir to shrink context

Tips

  • Without .dockerignore, docker build . sends the whole dir to the daemon; node_modules and .git blow up the context.
  • tmp/ with a slash matches only root tmp; tmp without slash matches any level.
  • .dockerignore and .gitignore are independent; they can't share one file.
  • Use ** for cross-level dir matching, e.g. **/__pycache__ ignores all Python caches.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Aug 2, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us