DEV Community

Alex Spinov
Alex Spinov

Posted on

Cloud Native Buildpacks Has a Free API: Transform Source Code into Container Images

Cloud Native Buildpacks (CNB) automatically detect your source code, install dependencies, and build optimized container images — no Dockerfile required. Just point it at your code and get a production-ready image.

What Is Cloud Native Buildpacks?

CNB is a CNCF incubating project that transforms application source code into container images. It uses detection and build phases to automatically figure out what your app needs and produce minimal, secure, rebuildable OCI images.

Key Features:

  • No Dockerfile needed
  • Auto-detection of language and framework
  • Reproducible builds
  • Efficient layer caching and rebasing
  • Security patching via image rebasing
  • Supports 20+ languages
  • Bill of materials (SBOM) generation

Quick Start with Pack CLI

# Install pack CLI
brew install buildpacks/tap/pack

# Build a Node.js app (auto-detected)
pack build my-node-app --builder paketobuildpacks/builder-jammy-base

# Build a Python app
pack build my-python-app --builder paketobuildpacks/builder-jammy-base

# Build a Go app
pack build my-go-app --builder paketobuildpacks/builder-jammy-base

# Run the image
docker run -p 8080:8080 my-node-app
Enter fullscreen mode Exit fullscreen mode

Buildpacks API: Programmatic Image Building

Using the Pack Library (Go)

package main

import (
    "context"
    "github.com/buildpacks/pack/pkg/client"
)

func main() {
    packClient, _ := client.NewClient()

    err := packClient.Build(context.Background(), client.BuildOptions{
        Image:   "my-app:latest",
        Builder: "paketobuildpacks/builder-jammy-base",
        AppPath: "./my-app",
        Env: map[string]string{
            "BP_NODE_VERSION": "20",
        },
    })
    if err != nil {
        panic(err)
    }
}
Enter fullscreen mode Exit fullscreen mode

Using kpack on Kubernetes

# kpack builds images automatically in your cluster
apiVersion: kpack.io/v1alpha2
kind: Image
metadata:
  name: my-app
spec:
  tag: registry.example.com/my-app
  serviceAccountName: registry-sa
  builder:
    name: my-builder
    kind: ClusterBuilder
  source:
    git:
      url: https://github.com/myorg/my-app
      revision: main
  build:
    env:
      - name: BP_NODE_RUN_SCRIPTS
        value: build
Enter fullscreen mode Exit fullscreen mode

Python Integration

import subprocess
import json

def build_image(app_path, image_name, builder="paketobuildpacks/builder-jammy-base"):
    result = subprocess.run(
        ["pack", "build", image_name,
         "--builder", builder,
         "--path", app_path],
        capture_output=True, text=True
    )
    return result.returncode == 0

def inspect_image(image_name):
    result = subprocess.run(
        ["pack", "inspect", image_name, "--output", "json"],
        capture_output=True, text=True
    )
    return json.loads(result.stdout)

# Build
build_image("./my-app", "my-app:v1")

# Inspect buildpacks used
info = inspect_image("my-app:v1")
for bp in info.get("buildpacks", []):
    print(f"Buildpack: {bp['id']} v{bp['version']}")
Enter fullscreen mode Exit fullscreen mode

Rebase for Security Patches

# Rebase updates the OS layer without rebuilding
pack rebase my-app:latest
# This takes seconds instead of minutes!
Enter fullscreen mode Exit fullscreen mode

CNB vs Dockerfile

Feature Buildpacks Dockerfile
Setup Zero config Manual
Security patches Rebase (seconds) Full rebuild
Reproducibility Guaranteed Depends
SBOM Auto-generated Manual
Caching Smart layer cache Layer cache
Multi-language Auto-detect Manual

Resources


Need to scrape web data for your build pipelines? Check out my web scraping tools on Apify — production-ready actors for Reddit, Google Maps, and more. Questions? Email me at spinov001@gmail.com

Top comments (0)