Linux command
ko 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Build and publish
ko build [./cmd/app]
Build into the local Docker daemon
ko build --local [./cmd/app]
Build a multi-platform image
ko build --platform=linux/amd64,linux/arm64 [./cmd/app]
Resolve Kubernetes manifests
ko resolve -f [path/to/manifest.yaml]
Build, push, and apply
ko apply -f [path/to/manifest.yaml]
Run
ko run [./cmd/app]
Log in
ko login [registry.example.com] -u [username] -p [password]
Print the version
ko version
说明
ko is a container image builder dedicated to Go. It compiles a Go _main_ package with the local go toolchain, lays the resulting static binary on top of a small distroless base image, and produces an OCI image, all without spawning docker or executing a Dockerfile. Because the build is just go build, cross-compilation, reproducible builds, and Go module caching all work as they do outside of containers. A typical workflow stores a fragment of YAML such as _image: ko://github.com/me/app/cmd/server_ in a Kubernetes manifest. ko resolve reads the manifest, runs ko build for every such reference, pushes the resulting images to $KO_DOCKER_REPO, and emits a new manifest where each ko:// reference has been rewritten to an immutable digest. ko apply does the same and pipes the output to kubectl apply, giving GitOps-style deployment in a single command. Images include an SBOM by default (SPDX) and are reproducible across machines because the Go binary, base image digest, and entrypoint are the only inputs. Multi-platform images are built in parallel and assembled into an OCI image index.
参数
- -B, --base-import-paths
- Tag images with only the final path component (e.g. _app_).
- -P, --preserve-import-paths
- Tag images with the full import path.
- --bare
- Use _$KO_DOCKER_REPO_ as the image name verbatim, with no suffix.
- --platform _list_
- Comma-separated platforms (e.g. _linux/amd64,linux/arm64,linux/arm/v7_). Use _all_ for every platform the base image supports.
- -t, --tags _list_
- Comma-separated tags applied to the published image (default: _latest_).
- -L, --local
- Load the built image into the local Docker daemon instead of pushing.
- --image-refs _file_
- Write the published image references to _file_, one per line.
- --sbom _format_
- SBOM format to embed: _spdx_ (default), _cyclonedx_, _go.version-m_, or _none_.
- --push _bool_
- When _false_, build without pushing. Useful with --tarball.
- --tarball _file_
- Write the image to a tarball loadable by docker load.
- --kubeconfig _file_
- Path to a kubeconfig used by apply, create, run, delete.
- --selector _label=value_
- Apply only to resources matching the label selector.
FAQ
What is the ko command used for?
ko is a container image builder dedicated to Go. It compiles a Go _main_ package with the local go toolchain, lays the resulting static binary on top of a small distroless base image, and produces an OCI image, all without spawning docker or executing a Dockerfile. Because the build is just go build, cross-compilation, reproducible builds, and Go module caching all work as they do outside of containers. A typical workflow stores a fragment of YAML such as _image: ko://github.com/me/app/cmd/server_ in a Kubernetes manifest. ko resolve reads the manifest, runs ko build for every such reference, pushes the resulting images to $KO_DOCKER_REPO, and emits a new manifest where each ko:// reference has been rewritten to an immutable digest. ko apply does the same and pipes the output to kubectl apply, giving GitOps-style deployment in a single command. Images include an SBOM by default (SPDX) and are reproducible across machines because the Go binary, base image digest, and entrypoint are the only inputs. Multi-platform images are built in parallel and assembled into an OCI image index.
How do I run a basic ko example?
Run `ko build [./cmd/app]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -B, --base-import-paths do in ko?
Tag images with only the final path component (e.g. _app_).