---
title: "Classic Builder vs BuildKit: Features and Differences"
date: "2026-11-18T15:15:00+07:00"
description: "Comparing Docker's classic image builder against BuildKit, analyzing parallel execution, secrets mounting, cache mounts, and performance gains."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/classic-builder-vs-buildkit.jpg"
alt: "Comparison chart showing sequential legacy Docker build vs parallel DAG BuildKit execution"
tags: ['docker', 'buildkit', 'performance', 'architecture']
---

For years, Docker used a sequential, single-threaded engine known as the **Classic Builder** to process Dockerfiles. With Docker Engine 23.0+, **BuildKit** became the default build engine for all Docker operations.

BuildKit is not just an incremental update—it is a complete architectural redesign of how container images are constructed, offering massive build speed improvements, advanced caching, parallel execution, and secure secrets handling.

In this deep dive, we will compare the Classic Builder against BuildKit, explore key feature differences, and understand why BuildKit is essential for modern container pipelines.

## Architectural Comparison: Sequential vs DAG Execution

```text
Classic Builder (Sequential Step-by-Step):
  [FROM base] --> [COPY package.json] --> [RUN npm install] --> [COPY .] --> [RUN npm build]
  (Executes top-to-bottom sequentially; un-used stages in multi-stage builds are STILL executed!)

BuildKit (Directed Acyclic Graph - DAG Engine):
  [Stage A: frontend] ──────┐
                            ├──> Executed IN PARALLEL! ──> [Final Stage]
  [Stage B: backend]  ──────┘
  (Skips un-referenced build stages entirely; executes independent steps concurrently!)
```

### 1. Sequential Execution vs Directed Acyclic Graph (DAG)
- **Classic Builder**: Executes every Dockerfile instruction strictly in linear order from line 1 to the end. In a multi-stage Dockerfile with 4 stages, it executes all 4 stages even if only 1 target stage is requested!
- **BuildKit**: Parses the Dockerfile into a Directed Acyclic Graph (DAG) using Low-Level Definition (LLB). It analyzes dependencies between stages, executes non-dependent stages **in parallel**, and completely skips unused stages.

### 2. High-Performance Build Context Transfer
- **Classic Builder**: Tar-compresses the entire build context directory and uploads it to the Docker daemon before building starts.
- **BuildKit**: Transfers context files **on demand** using a gRPC stream. If a file isn't needed by a layer, it is never sent across the network!

## Key Features Exclusive to BuildKit

| Feature | Classic Builder | BuildKit |
| :--- | :--- | :--- |
| **Execution Model** | Linear / Sequential | Parallel DAG Solver |
| **Context Transfer** | Full directory tar upload | On-demand file streaming |
| **Unused Stage Pruning**| No (executes all steps) | Yes (prunes unused stages) |
| **Build Secrets** | Hardcoded or via `ARG` (leaked) | `RUN --mount=type=secret` (secure) |
| **Package Caching** | Layer-level caching only | `RUN --mount=type=cache` (persistent) |
| **SSH Forwarding** | Not supported | `RUN --mount=type=ssh` |
| **OCI Attestations** | Not supported | Provenance & SBOM generation |

## Deep Dive into BuildKit Mount Innovations

BuildKit introduces powerful `RUN --mount` options that solve long-standing Dockerfile pain points:

### 1. Cache Mounts (`type=cache`)
Cache package manager downloads across builds without bloating final image layers:

```dockerfile
# BuildKit persistent cache mount for Go module downloads
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    go build -o server .
```

### 2. Build Secrets (`type=secret`)
Mount temporary API tokens or private keys during a `RUN` instruction without baking them into layer history or container metadata:

```dockerfile
# Securely fetch private npm package during build
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
    npm install
```

Build execution command:
```bash
docker build --secret id=npmrc,src=$HOME/.npmrc .
```

### 3. SSH Agent Forwarding (`type=ssh`)
Pass host SSH credentials into the builder to pull private Git repositories without copying private keys into the container:

```dockerfile
RUN --mount=type=ssh git clone git@github.com:myorg/private-repo.git
```

Build execution command:
```bash
docker build --ssh default .
```

## How to Enable BuildKit

If running older Docker Engine versions (prior to v23.0), enable BuildKit using an environment variable:

```bash
export DOCKER_BUILDKIT=1
docker build -t myapp:latest .
```

Or enable it permanently in `/etc/docker/daemon.json`:

```json
{
  "features": {
    "buildkit": true
  }
}
```

## Summary

BuildKit transforms container building from a slow sequential task into a fast, parallelized, secure workflow. Mastering BuildKit features like DAG execution, cache mounts, and build secrets is essential for modern DevOps engineers.
