Skip to content
Back to notes

Docker build cache: the .dockerignore gotcha

Docker builds slow despite a clean layer order? Your .dockerignore is probably letting files bust the cache on every commit. The two-line fix.

1 min read

Spent 2 hours debugging why my Docker builds were slow despite using multi-stage builds and proper layer ordering.

the issue

Every single build was invalidating the cache at the COPY . . step, even when I hadn't changed any code.

the culprit

My editor was creating .swp files and updating file timestamps. Docker saw these changes and invalidated the cache.

the fix

Add a proper .dockerignore:

bash
.git
.gitignore
README.md
.env*
node_modules
npm-debug.log
.next
.vscode
*.swp
*.swo
.DS_Store

Build time went from 5 minutes to 30 seconds.

Treat .dockerignore like .gitignore. Be aggressive about what you exclude.

Adjacent notes
TIL: Docker build cache: the .dockerignore gotcha