# moby/buildkit architecture

> Architecture diagram of the GitHub repository moby/buildkit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/moby/buildkit
- Repository: https://github.com/moby/buildkit

## Overview

##### Main Components Overview
The system should be divided into these major components:

a) Core Components:
- Frontend System (frontend/*)
- Dockerfile Frontend
- Gateway Frontend
- Other Language Frontends
- Solver Engine (solver/*)
- Cache System (cache/*)
- Worker System (worker/*)
- Control Plane (control/*)

b) Client Components:
- BuildKit Client (client/*)
- LLB (Low-Level Builder) System
- Command Line Interface (cmd/*)

##### Key Relationships to Highlight

a) Client-Server Flow:
**buildctl (Client) -> gRPC -> buildkitd (Server)**

b) Build Process Flow:
**Frontend -> LLB -> Solver -> Worker -> Output**

##### Detailed Component Breakdown

a) Frontend Layer:
- How they convert build definitions to LLB
- Frontend attestations system

b) Solver Layer:
- Vertex and edge handling
- Cache integration points
- Parallel execution paths

c) Worker Layer:
- Both containerd and runc worker implementations
- Executor subsystem
- Snapshot management

d) Cache System:
- Distributed caching
- Different cache exporters (inline, registry, local)
- Garbage collection system

##### Implementation Details

Use these visual elements:
- Blue: Core services
- Green: Client components
- Yellow: Cache systems
- Red: Security boundaries
- Gray: External services

- Connection Types:

- Rectangles: Core services
- Hexagons: External interfaces
- Clouds: Remote services

##### External Integrations

Connections to:
- Container registries
- Source code repositories
- Remote cache storage
- CI/CD systems

##### Operational Flows

Operational sequences:
- Build request flow
- Cache import/export flow
- Frontend conversion process
- Output generation path

This system design should emphasize BuildKit's key features: parallel execution, caching efficiency, pluggable architecture, and security considerations, while showing how all components work together to create a flexible and powerful build system.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        CLI["Command Line Interface"]:::client
        BKCL["BuildKit Client"]:::client
        LLB["LLB System"]:::client
    end

    subgraph "Frontend Layer"
        DF["Dockerfile Frontend"]:::frontend
        GF["Gateway Frontend"]:::frontend
        FA["Frontend Attestations"]:::frontend
    end

    subgraph "Core Services"
        subgraph "Solver Layer"
            SE["Solver Engine"]:::solver
            SC["Solver Cache"]:::solver
            SS["Solver Scheduler"]:::solver
        end

        subgraph "Worker Layer"
            CW["Containerd Worker"]:::worker
            RW["Runc Worker"]:::worker
            BW["Base Worker"]:::worker
        end

        subgraph "Cache System"
            RC["Remote Cache"]:::cache
            CH["Content Hash"]:::cache
            MD["Metadata"]:::cache
        end

        subgraph "Control Layer"
            CG["Control Gateway"]:::control
            CS["Control Service"]:::control
        end

        subgraph "Session Management"
            FS["File Sync"]:::session
            SSH["SSH Forward"]:::session
            SEC["Secrets"]:::session
        end

        subgraph "Source Providers"
            GIT["Git Source"]:::source
            HTTP["HTTP Source"]:::source
            CI["Container Image"]:::source
            LS["Local Source"]:::source
        end
    end

    %% Relationships
    CLI --> BKCL
    BKCL --> LLB
    LLB --> CG

    DF & GF & FA --> SE
    SE --> SC
    SE --> SS
    SS --> CW & RW
    CW & RW --> BW

    SE -.-> RC
    RC --> CH
    CH --> MD

    CG --> CS
    CS --> SE

    FS & SSH & SEC --> CS

    GIT & HTTP & CI & LS --> SE

    %% Click Events
    click CLI "https://github.com/moby/buildkit/tree/master/cmd/buildctl"
    click BKCL "https://github.com/moby/buildkit/tree/master/client"
    click LLB "https://github.com/moby/buildkit/tree/master/client/llb"
    click DF "https://github.com/moby/buildkit/tree/master/frontend/dockerfile"
    click GF "https://github.com/moby/buildkit/tree/master/frontend/gateway"
    click FA "https://github.com/moby/buildkit/tree/master/frontend/attestations"
    click SE "https://github.com/moby/buildkit/tree/master/solver/llbsolver"
    click SC "https://github.com/moby/buildkit/tree/master/solver/bboltcachestorage"
    click SS "https://github.com/moby/buildkit/blob/master/solver/scheduler.go"
    click CW "https://github.com/moby/buildkit/tree/master/worker/containerd"
    click RW "https://github.com/moby/buildkit/tree/master/worker/runc"
    click BW "https://github.com/moby/buildkit/tree/master/worker/base"
    click RC "https://github.com/moby/buildkit/tree/master/cache/remotecache"
    click CH "https://github.com/moby/buildkit/tree/master/cache/contenthash"
    click MD "https://github.com/moby/buildkit/tree/master/cache/metadata"
    click CG "https://github.com/moby/buildkit/tree/master/control/gateway"
    click CS "https://github.com/moby/buildkit/tree/master/api/services/control"
    click FS "https://github.com/moby/buildkit/tree/master/session/filesync"
    click SSH "https://github.com/moby/buildkit/tree/master/session/sshforward"
    click SEC "https://github.com/moby/buildkit/tree/master/session/secrets"
    click GIT "https://github.com/moby/buildkit/tree/master/source/git"
    click HTTP "https://github.com/moby/buildkit/tree/master/source/http"
    click CI "https://github.com/moby/buildkit/tree/master/source/containerimage"
    click LS "https://github.com/moby/buildkit/tree/master/source/local"

    %% Styles
    classDef client fill:#90EE90
    classDef frontend fill:#ADD8E6
    classDef solver fill:#4169E1
    classDef worker fill:#4169E1
    classDef cache fill:#FFD700
    classDef control fill:#4169E1
    classDef session fill:#D3D3D3
    classDef source fill:#FFA07A

    %% Legend
    subgraph Legend
        L1[Client Components]:::client
        L2[Frontend Services]:::frontend
        L3[Core Services]:::solver
        L4[Cache Systems]:::cache
        L5[Session Services]:::session
        L6[Source Providers]:::source
    end
```
