Back to TILs
Today I Learned

Docker Build Cache: The .dockerignore Gotcha

December 5, 2024
1 min read
---

# 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:

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.

  • *Pro tip:** Treat `.dockerignore` like `.gitignore` - be aggressive about what you exclude!

More TILs You Might Like

TIL: Docker Build Cache: The .dockerignore Gotcha | Harshit Luthra