DEV Community

Harshit Luthra
Harshit Luthra

Posted on • Originally published at harshit.cloud

Docker Build Cache: The .dockerignore Gotcha

Originally published at harshit.cloud on 2024-12-05.


TIL: Docker Build Cache: The .dockerignore Gotcha

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:

.git
.gitignore
README.md
.env*
node_modules
npm-debug.log
.next
.vscode
*.swp
*.swo
.DS_Store
Enter fullscreen mode Exit fullscreen mode

Build time went from 5 minutes to 30 seconds.

Pro tip: treat .dockerignore like .gitignore. Be aggressive about what you exclude.

Top comments (0)