Test Your Optimization Skills: Docker Build Speed Run
Drag and reorder layers in the browser to turn a 14m bloat monster into a 6s micro-container.
Lab Overview & Prerequisites
In this lab, you will benchmark three versions of an enterprise Node.js & Python application:
- Baseline (Unoptimized): Single-stage build with full context copy before dependency resolution.
- Stage 2 (Layer Ordered): Strict separation of dependency manifests and source files.
- Stage 3 (BuildKit & Distroless): Multi-stage build with
--mount=type=cacheand a non-root distroless runtime.
Step 1: Enable Docker BuildKit Engine
Ensure your terminal session activates BuildKit by default:
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Verify BuildKit engine status
docker buildx version
Step 2: Benchmark Cold Build vs. Hot Edit
Create a benchmark script to measure elapsed wall-clock build time:
# Measure cold build without cache
time docker build --no-cache -t myapp:cold .
# Simulate minor code edit
touch src/index.js
# Measure cached incremental build
time docker build -t myapp:incremental .
Step 3: Inspect Bloated Layers with Dive
Use the open-source dive CLI utility to explore where gigabytes are wasted inside your image:
# Install dive on macOS / Linux
brew install dive
# Analyze image layer efficiency
dive myapp:cold
Look specifically for the Image Efficiency Score. Unoptimized images will show duplicated files and wasted space in /root/.npm or /var/lib/apt/lists.
Step 4: Implement BuildKit Cache Mounts
Update your Dockerfile header to activate BuildKit syntax 1.4+ and mount host caches:
# syntax=docker/dockerfile:1.4
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
# Mount npm cache across builds without baking it into the image layer
RUN --mount=type=cache,target=/root/.npm npm ci
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY src/ ./src
USER node
CMD ["node", "src/index.js"]
Step 5: Benchmark Results Comparison
After implementing layer reordering and BuildKit cache mounts, compare your metrics:
- Cold Build: 12m 45s → 34s (BuildKit parallel downloads)
- Incremental Code Edit: 6m 12s → 1.8s (100% layer cache hit)
- Image Size: 1,480 MB → 42 MB (Multi-stage distillation)
Ready to Practice in the Arcade?
Challenge your colleagues in the 60-second Docker Build Speed Run drill.