ko — Build and Deploy Go Container Images Without Dockerfiles
ko builds Go applications into container images without a Dockerfile or Docker daemon, producing minimal images and deploying them directly to Kubernetes with a single command.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 3e1fc6d3-3987-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
ko is a build tool for Go applications that creates container images without requiring a Dockerfile or a running Docker daemon. It compiles your Go binary and layers it onto a minimal distroless base image, producing small, secure containers. ko can also deploy directly to Kubernetes by replacing image references in YAML manifests with the built image.
ko is designed for Go developers who want fast, simple container builds without writing or maintaining Dockerfiles.
How it saves time or tokens
Traditional container builds for Go apps require writing a Dockerfile, building a Docker image, pushing to a registry, and updating Kubernetes manifests. ko collapses all of that into a single command. ko build ./cmd/myapp compiles, builds the image, and pushes it. ko apply -f deployment.yaml goes further by building the image and applying the manifest to your cluster in one step. Build times are faster because ko skips the Docker layer cache dance and builds only the Go binary.
How to use
- Install ko:
go install github.com/ko-build/ko@latest
- Set your container registry:
export KO_DOCKER_REPO=ghcr.io/myorg
- Build and push an image:
ko build ./cmd/myapp
Or build and deploy to Kubernetes:
ko apply -f deploy/
ko replaces ko://./cmd/myapp references in your YAML with the actual image digest.
Example
A Kubernetes deployment manifest using ko image references:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: ko://./cmd/myapp
ports:
- containerPort: 8080
Running ko apply -f deploy.yaml builds the Go binary, creates a container image, pushes it to your registry, replaces ko://./cmd/myapp with the digest reference, and applies the manifest to your cluster.
Related on TokRepo
- DevOps tools — Browse container and deployment tools
- Automation tools — Explore build automation tools
Common pitfalls
- Forgetting to set
KO_DOCKER_REPO. Without this environment variable, ko does not know which registry to push images to and will fail. - Expecting ko to work with non-Go applications. ko is specifically designed for Go. For other languages, you still need Dockerfiles or language-specific build tools.
- Not using the
.ko.yamlconfig file for custom base images. The default distroless base is minimal but may lack tools you need for debugging. Configure a custom base image in.ko.yamlif needed. - Starting with an overly complex configuration instead of defaults. Begin with the minimal setup, verify it works, then customize incrementally. This approach catches configuration errors early and keeps troubleshooting straightforward.
常见问题
ko uses Google's distroless base image by default, which contains only the Go binary and CA certificates. This produces images under 20MB. You can override the base image in a .ko.yaml configuration file.
Yes. ko supports building multi-platform images (linux/amd64, linux/arm64) with the --platform flag. It cross-compiles the Go binary for each target platform and creates a manifest list.
Yes. Use ko build --local to build the image and load it into your local Docker daemon without pushing. This is useful for local development and testing.
ko is typically faster because it skips Docker layer caching and builds only the Go binary. A ko build that takes 5 seconds might take 30+ seconds with Docker. The difference is more pronounced in CI environments where Docker layer caches are cold.
Yes. ko uses the standard Go toolchain and respects go.mod, go.sum, and all Go build flags. Any Go project that builds with go build will work with ko.
引用来源 (3)
- ko GitHub— ko builds Go container images without Dockerfile
- Google Distroless— Distroless base images by Google
- ko Documentation— ko documentation and configuration
TokRepo 相关
讨论
相关资产
Earthly — Build Automation for the Container Era
Earthly combines the best of Dockerfiles and Makefiles into a single repeatable build tool. Define builds in an Earthfile, and Earthly caches, parallelizes, and runs them identically on laptop or CI.
Serverless Framework — Build and Deploy Serverless Apps to Any Cloud
The most widely adopted toolkit for building serverless applications on AWS Lambda, Azure Functions, Google Cloud Functions, and more. Define infrastructure and functions in a single YAML file and deploy with one command.
Skopeo — Registry-Agnostic Container Image Toolkit
Skopeo inspects, copies, signs, and deletes container images across registries without a daemon — the Swiss Army knife for OCI image plumbing in CI pipelines.
imgproxy — Fast Secure Image Processing Server in Go
Resize, crop, and convert images on-the-fly with imgproxy. A blazing-fast Go server powered by libvips for production-grade image transformation at scale.