DEV Community

Alex Spinov
Alex Spinov

Posted on

ko Has a Free API: Build Go Container Images Without a Dockerfile

Why ko Exists

If you write Go services, you do not need a Dockerfile. ko builds minimal container images directly from Go source code — no Docker daemon, no multi-stage builds, just ko build ./cmd/app.

Install

go install github.com/ko-build/ko@latest
Enter fullscreen mode Exit fullscreen mode

Build and Push in One Command

# Set your registry
export KO_DOCKER_REPO=ghcr.io/myorg

# Build and push
ko build ./cmd/server
# Output: ghcr.io/myorg/server@sha256:abc123...

# Build for local development
ko build ./cmd/server --local
Enter fullscreen mode Exit fullscreen mode

Deploy to Kubernetes

ko resolves image references in YAML:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: app
          image: ko://github.com/myorg/myapp/cmd/server
Enter fullscreen mode Exit fullscreen mode
# Build image AND apply to cluster
ko apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Multi-Platform Builds

# Build for linux/amd64 and linux/arm64
ko build ./cmd/server --platform=linux/amd64,linux/arm64
Enter fullscreen mode Exit fullscreen mode

GitHub Actions

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: "1.22"
      - uses: ko-build/setup-ko@v0.7
      - run: ko build ./cmd/server
        env:
          KO_DOCKER_REPO: ghcr.io/${{ github.repository }}
Enter fullscreen mode Exit fullscreen mode

Configuration (.ko.yaml)

defaultBaseImage: cgr.dev/chainguard/static:latest
builds:
  - id: server
    main: ./cmd/server
    env:
      - CGO_ENABLED=0
    flags:
      - -trimpath
    ldflags:
      - -s -w
Enter fullscreen mode Exit fullscreen mode

Why ko Over Dockerfile

Feature ko Dockerfile
Build time ~5 sec 30-60 sec
Image size ~10MB 50-500MB
Base image distroless/static You configure
Caching Go module cache Layer cache
K8s integration Native (ko apply) Separate step
SBOM Built-in Manual

Resources


Need to extract Go module data, container registry info, or build metrics? Check out my Apify tools or email spinov001@gmail.com for custom data extraction.

Top comments (0)