containers

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package containers wraps Docker and Podman command-line runtimes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Detect

func Detect(lookPath func(string) (string, error)) string

Detect returns the preferred container runtime name.

func EnvWithOverrides

func EnvWithOverrides(base, overrides []string) []string

EnvWithOverrides applies NAME=value overrides to base.

func RedactCommandArgsForLog

func RedactCommandArgsForLog(args []string) []string

RedactCommandArgsForLog returns args with sensitive values redacted.

Types

type Container

type Container struct {
	Name      string
	State     string
	CreatedAt time.Time
	Labels    map[string]string
	SSHPort   int32
	VNCPort   int32
}

Container describes normalized Docker/Podman container metadata.

func ParseInspectContainer

func ParseInspectContainer(data []byte) (*Container, error)

ParseInspectContainer parses Docker/Podman inspect JSON into container metadata.

type Event

type Event struct {
	Name       string
	Attributes map[string]string
}

Event describes a Docker/Podman lifecycle event.

type InspectInfo

type InspectInfo struct {
	Runtime      string
	ID           string
	Name         string
	State        string
	ImageRef     string
	ImageID      string
	Platform     string
	OS           string
	Architecture string
	CPULimit     int
	Mounts       []Mount
	Labels       map[string]string
}

InspectInfo describes observed Docker/Podman runtime configuration for a container.

type Logger

type Logger interface {
	Log(ctx context.Context, level slog.Level, msg string, args ...any)
}

Logger receives structured log records.

type Mount

type Mount struct {
	HostPath      string
	ContainerPath string
	ReadOnly      bool
}

Mount describes an observed runtime mount.

type Runtime

type Runtime interface {
	// Name returns the normalized runtime name, such as "docker" or "podman".
	Name() string
	// Executable returns the runtime command used for execution.
	Executable() string
	// Run executes a runtime command and returns trimmed stdout.
	Run(ctx context.Context, dir string, args ...string) (string, error)
	// RunOut executes a runtime command with stdout and stderr connected to writers.
	RunOut(ctx context.Context, dir string, stdout, stderr io.Writer, args ...string) error

	// List returns all containers known to the runtime.
	List(ctx context.Context) ([]Container, error)
	// InspectContainer returns normalized inspect metadata for one container.
	InspectContainer(ctx context.Context, name string) (*Container, error)
	// InspectInfo returns detailed normalized inspect metadata for one container.
	InspectInfo(ctx context.Context, name string) (*InspectInfo, error)
	// HostPort returns the host port mapped to containerPort.
	HostPort(ctx context.Context, containerName, containerPort string) (int32, error)

	// ImageArchitecture returns the linux architecture for image when available.
	ImageArchitecture(ctx context.Context, image string) (string, error)
	// RemoteManifestDigest queries the registry for the per-architecture manifest digest without downloading layers.
	RemoteManifestDigest(ctx context.Context, image, arch string) (string, error)
	// BaseImageIsLocal reports whether image resolves as a local image tag.
	BaseImageIsLocal(ctx context.Context, image string) bool
	// UntagImage removes an image tag.
	UntagImage(ctx context.Context, image string) error

	// Stats returns current resource usage for name.
	Stats(ctx context.Context, name string) (*Stats, error)
	// DiskUsage returns the writable container layer size in bytes.
	DiskUsage(ctx context.Context, name string) (int64, error)
	// StatsAll fetches resource usage for multiple containers in batch.
	StatsAll(ctx context.Context, names []string) (map[string]*Stats, error)
	// WatchStats streams resource usage for the named running containers.
	WatchStats(ctx context.Context, names []string) (iter.Seq2[StatsSample, error], error)
	// WatchDieEvents streams container die events for containers carrying labelKey.
	WatchDieEvents(ctx context.Context, labelKey string) (iter.Seq2[Event, error], error)
	// IsRootless reports whether the runtime is running rootless.
	IsRootless() bool
}

Runtime is a Docker-compatible container runtime.

func New

func New(executable string, logger Logger, env []string) (Runtime, error)

New returns a runtime wrapper for executable.

type Stats

type Stats struct {
	CPUPerc    float64 `json:"cpu_perc"`
	MemUsed    uint64  `json:"mem_used"`
	MemLimit   uint64  `json:"mem_limit"`
	MemPerc    float64 `json:"mem_perc"`
	PIDs       int     `json:"pids"`
	NetRx      uint64  `json:"net_rx"`
	NetTx      uint64  `json:"net_tx"`
	BlockRead  uint64  `json:"block_read"`
	BlockWrite uint64  `json:"block_write"`
	DiskUsed   int64   `json:"disk_used"`
}

Stats holds runtime resource usage for a container.

type StatsSample

type StatsSample struct {
	Name  string
	Stats *Stats
}

StatsSample describes one streamed runtime stats sample.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL