Skip to content

Dockerfile DSL

internal/image/dockerfile builds every image's Dockerfile from typed Go values instead of string templates. Each rendered instruction carries a comment with the Go file and line that produced it.

Type system

Everything implements Instruction:

type Instruction interface {
    Render() string
}

Instructions

instructions.go defines the standard directives:

Type Fields Renders as
From Image, As FROM image AS name
Arg Key, Default ARG key=default
Env Key, Value ENV key=value
Shell Cmd []string SHELL ["a", "b"] (exec form)
User Name USER name
Workdir Path WORKDIR /path
Label Key, Value LABEL key=value
Entrypoint Cmd []string ENTRYPOINT ["a", "b"] (exec form)
Copy From, Src, Dest COPY src dest (or COPY --from=stage src dest when From is set)

Run

Run (run.go) has two modes:

// Single pre-formatted string
Run{Command: "npm ci --omit=dev"}

// Grouped blocks, && joined; each block can carry an optional shell comment
Run{Blocks: []Block{
    {Lines: []string{"apt-get update -yq"}},
    {Lines: []string{"apt-get install -yq --no-install-recommends", "curl", "wget"}},
    {Lines: []string{"rm -rf /var/lib/apt/lists/*"}},
}}

The Blocks form renders as:

RUN apt-get update -yq \
  && apt-get install -yq --no-install-recommends \
  curl \
  wget \
  && rm -rf /var/lib/apt/lists/*

Add a Comment to a block to insert a shell comment before its lines:

{Comment: "Create container user", Lines: []string{"useradd -m claude"}}

Heredoc

Heredoc (heredoc.go) writes a script with BuildKit's COPY --chmod=0755 <<'EOF', executable whatever the active USER:

Heredoc{
    Dest:  "/usr/local/bin/entrypoint.sh",
    Lines: []string{"#!/usr/bin/env bash", "set -euo pipefail", `exec claude "$@"`},
}

Renders as:

COPY --chmod=0755 <<'EOF' /usr/local/bin/entrypoint.sh
#!/usr/bin/env bash
set -euo pipefail
exec claude "$@"
EOF

Located

Located (located.go) wraps an Instruction with a comment naming its Go file:line. StageBuilder.Add() adds it for you. For a human-readable note, use a Block's Comment.

Stage and File

Stage holds one FROM block; File collects stages into a complete Dockerfile:

type Stage struct {
    GlobalArgs   []Arg         // ARG directives placed before FROM, used in the image spec
    From         From
    Instructions []Instruction
}

type File struct {
    Stages []Stage
}

File.Render() emits each stage with a ###### section divider and the stage name, global args before FROM, then instructions separated by blank lines.

Builder API

StageBuilder builds a Stage. Add() takes one instruction or several:

// Single instruction
Add(df.Env{Key: "DEBIAN_FRONTEND", Value: "noninteractive"})

// Multiple instructions in one call (share the same source location)
Add(df.Arg{Key: "HOST_UID", Default: "1000"}, df.Arg{Key: "HOST_GID", Default: "1000"})

// Spread a slice returned by a helper
Add(CreateContainerUser("claude")...)

Full example:

stage := df.NewStage(df.From{Image: "debian:${DEBIAN_VERSION}", As: "base"}).
    AddGlobalArg(df.Arg{Key: "DEBIAN_VERSION", Default: "bookworm-slim"}).
    Add(df.Env{Key: "DEBIAN_FRONTEND", Value: "noninteractive"}).
    Add(df.Run{Blocks: []df.Block{
        {Lines: []string{"apt-get update -yq"}},
        {Lines: []string{"apt-get install -yq --no-install-recommends", "curl", "wget", "git"}},
        {Lines: []string{"rm -rf /var/lib/apt/lists/*"}},
    }}).
    Build()

Partial rendered output:

##############################
# base
##############################
ARG DEBIAN_VERSION=bookworm-slim

FROM debian:${DEBIAN_VERSION} AS base

# internal/image/tools/bases.go:23
ENV DEBIAN_FRONTEND=noninteractive

# internal/image/tools/bases.go:28
RUN apt-get update -yq \
  && apt-get install -yq --no-install-recommends \
  curl \
  wget \
  git \
  && rm -rf /var/lib/apt/lists/*

Multi-stage composition

Each tool and extra stage is a function that returns a df.Stage built FROM the previous one:

func claudeStage(prevStage string, img ImageInfo) df.Stage {
    return df.NewStage(df.From{Image: prevStage, As: "tool"}).
        // ...
        Build()
}

internal/image/tools/generate.go chains them (base, extras, tool) and renders the File. A tool plugs in through the Stage field of its BuildConfig (internal/image/tools/tools.go).