containerd/containerd
pkg/oci
Helpers for building OCI runtime specs. The largest and most-used package in pkg/. It's where almost every "configure this aspect of the container" option ends up.
Purpose
- Construct an
oci-runtime-specSpecfrom defaults. - Provide composable
SpecOptsto layer in image config, mounts, namespaces, capabilities, cgroup constraints, … - Hide platform differences behind a single
Spectype.
Directory layout
pkg/oci/ contains roughly a hundred files split per-platform:
spec.go—Spectype alias and constructors (GenerateSpec,WithDefaultSpec, etc.)spec_opts.go— cross-platform optionsspec_opts_linux.go/spec_opts_windows.go/spec_opts_freebsd.go— platform optionsmounts*.go— mount option helpers (RO, propagation, tmpfs)client.go— minimalClientinterface so thatpkg/ocican fetch image config without depending onclient/
Key API
The spec is built by chaining SpecOpts:
spec, err := oci.GenerateSpec(ctx, client, container,
oci.WithImageConfig(image),
oci.WithProcessArgs("/bin/sh", "-c", "echo hi"),
oci.WithCapabilities([]string{"CAP_NET_ADMIN"}),
oci.WithMounts([]specs.Mount{...}),
oci.WithLinuxNamespace(specs.LinuxNamespace{Type: specs.UserNamespace, Path: "/proc/123/ns/user"}),
)Each SpecOpts is a func(ctx, client, container, *Spec) error. The composition is order-sensitive — later opts can override earlier ones.
Common opts
| Opt | What it sets |
|---|---|
WithImageConfig |
Pulls user, env, working dir, cmd from the image config |
WithProcessArgs |
The container's argv |
WithEnv |
Append env vars |
WithMounts |
Add mounts (with conflict resolution) |
WithRootFSPath |
Set the root path in the spec |
WithLinuxNamespace / WithoutLinuxNamespace |
Add or remove a Linux namespace |
WithCapabilities / WithAllCapabilities / WithDroppedCapabilities |
Capability set tweaks |
WithCgroup |
Set the cgroup path |
WithMemoryLimit, WithCPUShares, ... |
Resource constraints |
WithSeccompUnconfined |
Drop the default seccomp filter |
WithUserNamespace |
Add a userns and remap mounts |
Mount opt edge cases
pkg/oci's mount handling deduplicates by destination, applies propagation flags, and merges options. The tests in pkg/oci/spec_opts_test.go cover the corner cases (overlapping mounts, source resolution, etc.).
Used by
client/container_opts.go'sWithNewSpecfactory.internal/cri/sputil/to construct specs from CRI requests.cmd/ctr/commands/run/run.goforctr run.- Third-party embedders directly (the most common use case in nerdctl, kata-containers, etc.).
Entry points for modification
- New cross-platform option: add to
spec_opts.goand document. - New Linux-specific option: add to
spec_opts_linux.go. Add a no-op or appropriate stub inspec_opts_other.goif you want the option to be cross-platform-callable. - New mount option: see
mounts.gofor the dedup logic. - Spec defaults: edit
populateDefaultUnixSpec/populateDefaultWindowsSpecinspec.go.
For how the spec is consumed downstream, see runtime v2.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.