Docker Caching Guide π
Build Cache Optimization π¦
Layer Caching
# β
DependΓͺncias primeiro
COPY package*.json ./
RUN npm ci
# β
CΓ³digo fonte depois
COPY . .
RUN npm run build
BuildKit Cache
# β
Cache mount para build
RUN --mount=type=cache,target=/root/.cache/go-build \
go build -o app
# β
Cache mount para package managers
RUN --mount=type=cache,target=/root/.npm \
npm ci
Registry Cache π
Registry Caching
# Cache em registry remoto
docker buildx build \
--cache-from type=registry,ref=user/app:cache \
--cache-to type=registry,ref=user/app:cache \
.
# Pull com cache
docker pull --cache-from user/app:cache myapp
Multi-registry Cache
# docker-compose.yml
services:
app:
build:
context: .
cache_from:
- registry-1.example.com/myapp:cache
- registry-2.example.com/myapp:cache
Local Cache Management πΎ
Volume Cache
version: '3.8'
services:
app:
build: .
volumes:
# Cache de dependΓͺncias
- deps:/app/node_modules
# Cache de build
- build:/app/build
volumes:
deps:
build:
Bind Mount Cache
# Cache de desenvolvimento
docker run \
-v $(pwd):/app \
-v node_modules:/app/node_modules \
node:alpine npm install
Cache Invalidation β‘
Estratégias de Invalidação
# β
Cache bust com ARG
ARG CACHE_BUST=unknown
RUN echo "${CACHE_BUST}" && npm ci
# β
Cache com checksum
COPY package.json package-lock.json ./
RUN echo "$(md5sum package-lock.json)" > /tmp/hash \
&& npm ci
Cache Cleanup
# Limpar cache local
docker builder prune --filter until=24h
# Limpar volumes de cache
docker volume rm $(docker volume ls -q -f name=*cache*)
Development Cache π§
Hot Reload Cache
# docker-compose.dev.yml
services:
app:
volumes:
- .:/app
- node_modules:/app/node_modules
- build_cache:/app/.next
environment:
- NODE_ENV=development
volumes:
node_modules:
build_cache:
Multi-stage Cache
# Build cache
FROM node:alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
# Development
FROM node:alpine AS dev
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
CMD ["npm", "run", "dev"]
# Production build
FROM dev AS builder
RUN npm run build
# Production
FROM node:alpine
COPY --from=builder /app/dist ./dist
CMD ["npm", "start"]
Cache Best Practices β
DO's
[x] Use multi-stage builds
[x] Organize layers por mutabilidade
[x] Implemente BuildKit cache mounts
[x] Utilize registry caching
[x] Mantenha volumes de cache separados
DON'Ts
[ ] Misturar dependências com código
[ ] Ignorar .dockerignore
[ ] Cachear dados sensíveis
[ ] Acumular cache desnecessário
[ ] Usar cache em produção sem necessidade
Performance Matrix π
Tipo de Cache | Uso | Performance |
---|---|---|
Layer | Build | Alta |
BuildKit | Build | Muito Alta |
Registry | Distribuição | Média |
Volume | Runtime | Alta |
Bind Mount | Desenvolvimento | Média |
Waifu Cache Tips π«
Quick Reference π
Common Commands
# Rebuild sem cache
docker build --no-cache .
# Usar cache especΓfico
docker build --cache-from myapp:cache .
# Limpar cache
docker builder prune
# Inspecionar cache
docker system df -v
Cache Configuration
# .dockerignore
node_modules
npm-debug.log
Dockerfile
.git
.cache
# docker-compose.yml
services:
app:
build:
context: .
cache_from:
- myapp:cache
volumes:
- build-cache:/app/build
volumes:
build-cache:
16 abril 2025