.dockerignore Cheatsheet - Docker Build Context Ignore Rules

Without a proper .dockerignore the build context ships node_modules and .git into the daemon. Grouped by globs, directory ignores, differences from .gitignore and context slimming, with copy-ready examples.

Config Formats·26 commands·Last updated 2026-08-02
Back to Config Formats

基础语法 Basic Syntax 6

node_modules
忽略名为 node_modules 的文件或目录
*.log
忽略所有 .log 文件
tmp/
只忽略根目录下的 tmp 目录(带斜杠)
**/tmp
忽略任意层级的 tmp 目录
# comment
井号开头为注释行
空行被忽略

通配符 Glob 6

*
匹配任意数量非分隔符字符
?
匹配单个非分隔符字符
**
匹配任意层级目录
[abc]
匹配括号内任一字符
*.tmp
忽略所有 .tmp 文件
dist/*
忽略 dist 下一级所有内容(保留 dist 目录本身)

常见忽略项 Common Ignores 6

.git
不把 git 仓库打进构建上下文
node_modules
依赖目录,通常已在镜像内安装
.env
不要泄漏密钥到构建环境
Dockerfile
忽略自身(极少需要)
*.md
忽略文档类文件
.dockerignore
忽略自身不影响当前构建

与 .gitignore 差异 4

!node_modules/important
用 ! 取反,重新包含被忽略项
Dockerfile 不读 .gitignore
.dockerignore 与 .gitignore 相互独立
构建上下文 = docker build .
docker 先按 .dockerignore 裁剪上下文再发给 daemon
COPY 仍受路径限制
.dockerignore 只影响上下文,不限制 COPY 目标

瘦身技巧 Slimming 4

**/*.pyc
忽略所有编译缓存
.cache/
忽略本地缓存目录
coverage/
忽略测试覆盖率报告
tests/
忽略测试目录减小上下文

💡 Tips

  • Without .dockerignore, docker build . sends the whole directory to the daemon — node_modules and .git bloat the context.
  • A trailing slash (tmp/) matches only the root tmp; without it, tmp matches at any level.
  • .dockerignore and .gitignore are independent; do not share one file between them.
  • Use ** to match across levels, e.g. **/__pycache__ ignores every Python cache dir.

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

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

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it