Build Performance Profiling
What you'll learn: How to measure and analyze Docker build times to find which steps are slowing you down.
When your Docker builds feel sluggish, you need data to pinpoint where the time is actually going. Build performance profiling gives you visibility into each layer's execution time, helping you optimize the right things instead of guessing.
Understanding Build Time Analysis
Think of profiling like checking your car's fuel efficiency on different road segments—you want to know if highway driving or city traffic is burning more gas. Similarly, Docker builds have distinct phases (pulling base images, installing dependencies, copying files), and each consumes time differently.
Docker provides two primary tools for profiling:
docker history
This command shows you the size and creation metadata of each layer in a built image:
docker history your-image:tag
While it displays layer sizes, it doesn't show build time for each instruction—it's more useful for understanding image bloat than build speed.
BuildKit Metrics
BuildKit (Docker's modern build engine) provides detailed timing information. Enable it and capture metrics:
DOCKER_BUILDKIT=1 docker build --progress=plain . 2>&1 | tee build.log
The --progress=plain flag shows verbose output with timing for each step. You'll see lines like #8 [3/5] RUN apt-get update ... 12.4s, revealing exactly how long each instruction took.
Finding Bottlenecks
Look for steps consuming disproportionate time—often dependency installations or large file copies. These become your optimization targets using techniques you've learned: layer caching, cache mounts, or multi-stage builds.
Key Takeaway: Use BuildKit's --progress=plain output to measure actual build times per instruction, turning blind optimization into data-driven improvements.