Optimize Docker build cache for faster, more efficient builds using BuildKit caching strategies, layer management, and cache backends.
Optimize Docker build performance by effectively managing build cache, understanding cache invalidation, and implementing advanced caching strategies with BuildKit.
This skill guides you through Docker build cache optimization techniques to dramatically reduce build times in local development and CI/CD pipelines. You'll learn to leverage BuildKit's caching mechanisms, implement multi-stage builds efficiently, use cache mount types, configure external cache backends, and structure Dockerfiles for maximum cache reuse.
**Analyze current build cache behavior:**
**Key concepts:**
**Restructure Dockerfile for better cache hits:**
```dockerfile
COPY . /app
RUN npm install
COPY package.json package-lock.json /app/
RUN npm install
COPY . /app
```
**Best practices:**
**Use `--mount=type=cache` for package managers:**
```dockerfile
RUN --mount=type=cache,target=/root/.npm \
npm install
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt \
apt-get update && apt-get install -y build-essential
RUN --mount=type=cache,target=/go/pkg/mod \
go build -o app
```
**Enable BuildKit if not default:**
```bash
export DOCKER_BUILDKIT=1
docker build --progress=plain .
```
**For CI/CD: Use registry cache for build artifacts sharing:**
```bash
docker buildx build \
--cache-to type=registry,ref=myregistry.com/myapp:buildcache \
--cache-from type=registry,ref=myregistry.com/myapp:buildcache \
-t myregistry.com/myapp:latest \
--push .
```
**For GitHub Actions: Use GitHub Actions cache:**
```yaml
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
```
**For local development: Use local cache:**
```bash
docker buildx build \
--cache-to type=local,dest=/tmp/docker-cache \
--cache-from type=local,src=/tmp/docker-cache \
.
```
**Optimize multi-stage builds for cache efficiency:**
```dockerfile
FROM node:20 AS deps
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
FROM node:20 AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
FROM node:20-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=deps /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
```
**Benefits:**
**Mount secrets without leaking into cache:**
```dockerfile
ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc && \
npm install && \
rm .npmrc
RUN --mount=type=secret,id=npm_token \
echo "//registry.npmjs.org/:_authToken=$(cat /run/secrets/npm_token)" > .npmrc && \
npm install
```
**Build with secret:**
```bash
docker buildx build --secret id=npm_token,src=$HOME/.npmrc .
```
**Analyze build cache effectiveness:**
```bash
docker buildx build --progress=plain .
docker history myimage:latest
docker buildx prune -f
docker buildx prune --filter type=exec.cachemount
```
**Look for:**
**Copy only what's needed, when needed:**
```dockerfile
COPY package.json package-lock.json ./
RUN npm ci --production
COPY src/ ./src/
COPY public/ ./public/
RUN npm run build
```
**Use .dockerignore:**
```
node_modules
.git
.env*
*.md
.dockerignore
Dockerfile
.gitignore
dist/
coverage/
```
**GitHub Actions example:**
```yaml
uses: docker/setup-buildx-action@v3
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: myregistry.com/myapp:${{ github.sha }}
cache-from: |
type=registry,ref=myregistry.com/myapp:buildcache
type=gha
cache-to: type=gha,mode=max
```
**GitLab CI example:**
```yaml
build:
image: docker:latest
services:
- docker:dind
before_script:
- docker buildx create --use
script:
- docker buildx build
--cache-from type=registry,ref=$CI_REGISTRY_IMAGE:buildcache
--cache-to type=registry,ref=$CI_REGISTRY_IMAGE:buildcache,mode=max
--push
-t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
```
**Track build performance improvements:**
**Key metrics:**
**Before optimization:**
```dockerfile
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
CMD ["npm", "start"]
```
**After optimization:**
```dockerfile
FROM node:20 AS deps
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
FROM node:20 AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
FROM node:20-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=deps /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
```
**Result: 84% faster incremental builds**
Based on Docker official documentation: https://docs.docker.com/build/cache/
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/docker-build-cache-optimization/raw