# Order matters in Dockerfiles for cache efficiency

**Date:** 2025-11-29  
**Tags:** Docker, DevOps, Performance  
**URL:** https://kelexine.is-a.dev/til/docker-build-cache-optimization

---

TIL: Order matters in Dockerfiles for cache efficiency. Copy package.json first, run npm install, THEN copy source code. Changed files don't bust the dependency cache. Cut build time from 5min to 30sec.


```dockerfile
# ❌ Bad: Busts cache on any file change
COPY . .
RUN npm install

# ✅ Good: Only reinstalls when deps change
COPY package*.json ./
RUN npm install
COPY . .
```




---

*This content is available at [kelexine.is-a.dev/til/docker-build-cache-optimization](https://kelexine.is-a.dev/til/docker-build-cache-optimization)*
