.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.
Basic Syntax 6
node_modulesIgnore a file or dir named node_modules
*.logIgnore all .log files
tmp/Only ignore tmp at root (with slash)
**/tmpIgnore tmp at any level
# commentHash 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
*.tmpIgnore all .tmp files
dist/*Ignore dist's direct children (keep dist itself)
Common Ignores 6
.gitDon't bake the git repo into the build context
node_modulesDependency dir, usually installed inside the image
.envDon't leak secrets into the build env
DockerfileIgnore itself (rarely needed)
*.mdIgnore docs
.dockerignoreIgnoring itself doesn't affect the current build
vs .gitignore 4
!node_modules/importantUse ! to negate and re-include an ignored item
Dockerfile ignores .gitignoreDockerfile ignores .gitignore
Build context = docker build .Build context = docker build .
COPY is still path-limitedCOPY is still path-limited
Slimming Tips 4
**/*.pycIgnore 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