Help us shape the future of Go! Take the 2026 Go Developer Survey

container

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrContainerNotFound   = errors.New("container cli not found")
	ErrSystemNotRunning    = errors.New("container system not running")
	ErrInvalidOptions      = errors.New("invalid options provided")
	ErrContainerNameExists = errors.New("container with this name already exists")
)

Functions

func NormalizeNameservers

func NormalizeNameservers(raw []string) ([]string, error)

NormalizeNameservers validates literal DNS server addresses and returns their canonical string forms. Loopback, unspecified, multicast, and link-local addresses cannot identify a nameserver reachable from another container.

func ValidateRunOptions

func ValidateRunOptions(image string, opts CreateOptions) error

ValidateRunOptions checks the same create-time arguments that Run passes to the external CLI, without starting a container.

Types

type BuildOptions

type BuildOptions struct {
	// The build context path. Defaults to current directory when empty.
	ContextDir string
	// Name and optionally a tag in the name:tag format.
	Tag string
	// Name of the Dockerfile (Default is "Dockerfile").
	File string
	// Do not use cache when building the image.
	NoCache bool

	// Target OS for the image.
	OS string
	// Target architecture for the image: amd64 or arm64
	Arch []string
	// Target platform for the image in the format os/arch[/variant].
	Platform string
	// Build arguments in the format key=val.
	BuildArgs map[string]string
	// Tries to pull the latest base image from the registry.
	Pull bool
}

BuildOptions defines the options for building an image.

type Client

type Client struct {
	Bin string

	Container ContainerClient
	System    SystemClient
	Images    ImageClient
	Volumes   VolumeClient
	Networks  NetworkClient
}

func NewClient

func NewClient() (*Client, error)

func (*Client) Available

func (c *Client) Available() bool

func (*Client) SystemRunning

func (c *Client) SystemRunning(ctx context.Context) bool

type ContainerClient

type ContainerClient struct {
	// contains filtered or unexported fields
}

func NewContainerClient

func NewContainerClient(
	run func(ctx context.Context, args ...string) (string, error),
	runStreaming func(ctx context.Context, out io.Writer, args ...string) (string, error),
) ContainerClient

func (*ContainerClient) Delete

func (c *ContainerClient) Delete(ctx context.Context, opts DeleteOptions) (string, error)

Delete deletes one or more containers based on the provided options. Returns the ID of the deleted container(s) and an error if the operation fails.

func (*ContainerClient) InspectDetails

func (c *ContainerClient) InspectDetails(ctx context.Context, ids []string) ([]ContainerDetails, error)

InspectDetails returns the volume and network metadata needed by higher-level clients. It intentionally exposes only typed fields rather than the runtime's full inspect response.

func (*ContainerClient) List

func (c *ContainerClient) List(ctx context.Context, opts ListOptions) (string, error)

List returns a list of containers based on the provided options

func (*ContainerClient) ListSummaries

func (c *ContainerClient) ListSummaries(ctx context.Context, opts ListOptions) ([]ContainerSummary, error)

ListSummaries returns typed container metadata from the list command.

func (*ContainerClient) Logs

func (c *ContainerClient) Logs(ctx context.Context, opts LogsOptions, out io.Writer) (string, error)

Logs streams container logs to the provided writer.

func (*ContainerClient) Run

func (c *ContainerClient) Run(ctx context.Context, image string, opts CreateOptions) (bool, error)

Run creates and starts a new container based on the provided image and options. Returns true if the container was successfully created and started, or an error if the operation fails.

func (*ContainerClient) RunStreaming added in v0.1.2

func (c *ContainerClient) RunStreaming(ctx context.Context, image string, opts CreateOptions, out io.Writer) (bool, error)

RunStreaming creates and starts a container while forwarding runtime output, including automatic image-pull progress, to out. Output is also retained for command errors. Without a streaming runner, captured output is forwarded when the command finishes.

func (*ContainerClient) Start

func (c *ContainerClient) Start(ctx context.Context, id string) (string, error)

Start starts one stopped container.

func (*ContainerClient) Stop

func (c *ContainerClient) Stop(ctx context.Context, opts StopOptions) (string, error)

Stop stops one or more running containers based on the provided options. Returns the ID of the stopped container(s) and an error if the operation fails.

type ContainerDetails

type ContainerDetails struct {
	ID          string
	ImageDigest string
	VolumeNames []string
	Mounts      []ContainerMount
	Networks    []NetworkAttachment
}

ContainerDetails is the container metadata needed by higher-level clients after inspecting a container.

type ContainerMount

type ContainerMount struct {
	Type     MountType
	Source   string
	Target   string
	ReadOnly bool
}

ContainerMount describes one configured container mount returned by inspect.

type ContainerSummary

type ContainerSummary struct {
	ID     string
	Labels map[string]string
}

ContainerSummary is the information returned for a container by the list command.

type CreateOptions

type CreateOptions struct {
	// Name of the container to create
	Name string
	// Number of CPUs to allocate to the container
	CPUs uint
	// Amount of memory (1MiByte granularity), with optional K, M, G, T, or P suffix
	Memory string
	// Remove the container after it exits
	Rm bool
	// Port mappings
	Publish []PortMapping
	// Environment variables in KEY=VALUE (or KEY) format.
	Environment []string
	// Labels associates metadata with the container as KEY=VALUE pairs.
	Labels map[string]string
	// Nameservers identifies DNS server IP addresses to pass through repeated
	// --dns flags. Their order is preserved.
	Nameservers []string
	// Networks identifies the networks to attach when creating the container.
	Networks []string
	// Mounts configures filesystem mounts for the container.
	Mounts []Mount
	// Container init Process arguments, if any
	Arguments []string
}

CreateOptions defines the options for creating a container

type DeleteOptions

type DeleteOptions struct {
	// Delete all containers
	All bool
	// List of container IDs or names to delete, should be empty if All flag is true
	IDs []string
	// Force deletion of running containers
	Force bool
}

type ImageClient

type ImageClient struct {
	// contains filtered or unexported fields
}

func NewImageClient

func NewImageClient(
	run func(ctx context.Context, args ...string) (string, error),
	runStreaming func(ctx context.Context, out io.Writer, args ...string) (string, error),
) ImageClient

func (*ImageClient) Build

func (i *ImageClient) Build(ctx context.Context, opts BuildOptions, out io.Writer) (string, error)

Build builds an image, streams command output to the provided writer, and returns the image id created.

func (*ImageClient) Exists

func (i *ImageClient) Exists(ctx context.Context, reference string) (bool, error)

Exists reports whether an image reference is available in the local image store.

func (*ImageClient) InspectSummary

func (i *ImageClient) InspectSummary(ctx context.Context, reference string) (ImageSummary, bool, error)

InspectSummary returns the descriptor digest for a local image and whether the reference exists. It uses image inspect and therefore never pulls.

func (*ImageClient) List

func (i *ImageClient) List(ctx context.Context, quiet bool) (string, error)

List retrieves a list of images in JSON format. If the quiet flag is set to true, it returns only the image IDs separated by newlines.

type ImageSummary

type ImageSummary struct {
	Digest string
}

ImageSummary is the stable local image identity needed by higher-level clients when deciding whether an existing container uses the current image.

type ListOptions

type ListOptions struct {
	// Display both running and stopped containers
	All bool
	// Return only container IDs
	Quiet bool
}

ListOptions defines the options for listing containers

type LogsOptions

type LogsOptions struct {
	// Follow streams logs continuously until context cancellation or process exit.
	Follow bool
	// IDs are container IDs or names to fetch logs for.
	IDs []string
}

LogsOptions defines options for streaming or fetching container logs.

type Mount

type Mount struct {
	// Type identifies the storage backing for the mount.
	Type MountType
	// Source is the absolute host path for a bind mount or a volume name for a
	// named-volume mount. Tmpfs mounts do not have a source.
	Source string
	// Target is the absolute path where the mount appears in the container.
	Target string
	// ReadOnly prevents writes through the mount.
	ReadOnly bool
	// TmpfsSize is the tmpfs capacity in bytes. Zero uses the runtime default.
	TmpfsSize uint64
	// TmpfsMode is the tmpfs file mode expressed as an octal Unix-permission
	// string. An empty value uses the runtime default.
	TmpfsMode string
}

Mount configures one filesystem mount for a container.

type MountType

type MountType string

MountType identifies the storage backing used by a container mount.

const (
	// MountTypeBind shares a host directory with a container.
	MountTypeBind MountType = "bind"
	// MountTypeVolume attaches a named volume to a container.
	MountTypeVolume MountType = "volume"
	// MountTypeTmpfs attaches an in-memory filesystem to a container.
	MountTypeTmpfs MountType = "tmpfs"
)

type NetworkAttachment

type NetworkAttachment struct {
	Name      string
	Addresses []netip.Addr
}

NetworkAttachment describes a container's assigned addresses on one runtime network. Addresses do not include the CIDR prefixes reported by the container CLI's inspect output.

type NetworkClient

type NetworkClient struct {
	// contains filtered or unexported fields
}

NetworkClient manages user-defined networks through the container CLI.

func NewNetworkClient

func NewNetworkClient(run func(ctx context.Context, args ...string) (string, error)) NetworkClient

NewNetworkClient creates a NetworkClient using the supplied command runner.

func (*NetworkClient) Create

Create creates a user-defined network and returns the CLI output.

func (*NetworkClient) Delete

func (n *NetworkClient) Delete(ctx context.Context, names []string) (string, error)

Delete deletes the named networks.

func (*NetworkClient) Exists

func (n *NetworkClient) Exists(ctx context.Context, name string) (bool, error)

Exists reports whether a user-defined network exists.

func (*NetworkClient) Inspect

func (n *NetworkClient) Inspect(ctx context.Context, names []string) (string, error)

Inspect returns the JSON description of the requested networks.

func (*NetworkClient) InspectSummary

func (n *NetworkClient) InspectSummary(ctx context.Context, name string) (NetworkSummary, bool, error)

InspectSummary returns metadata for a network and whether it exists.

func (*NetworkClient) List

func (n *NetworkClient) List(ctx context.Context) (string, error)

List returns the JSON description of all networks.

func (*NetworkClient) ListSummaries

func (n *NetworkClient) ListSummaries(ctx context.Context) ([]NetworkSummary, error)

ListSummaries returns names and labels for every network.

type NetworkCreateOptions

type NetworkCreateOptions struct {
	// Name is the name of the network to create.
	Name string
	// Labels associates metadata with the network as KEY=VALUE pairs.
	Labels map[string]string
}

NetworkCreateOptions configures user-defined network creation.

type NetworkSummary

type NetworkSummary struct {
	Name   string
	Labels map[string]string
}

NetworkSummary is the network metadata used by higher-level clients to identify an ACC-managed network.

type PortMapping

type PortMapping struct {
	HostIP        string
	HostPort      uint16
	ContainerPort uint16
	Protocol      Protocol // "tcp" or "udp"
}

PortMapping defines a mapping from a host port to a container port as taken by the container cli

type Protocol

type Protocol string

Protocol types accepted by the container cli defined in the container package: TCP and UDP

const (
	TCP Protocol = "tcp"
	UDP Protocol = "udp"
)

type Signal

type Signal string

Signal defines the signal types accepted by the container cli defined under container package

const (
	SIGTERM Signal = "SIGTERM"
	SIGKILL Signal = "SIGKILL"
	SIGINT  Signal = "SIGINT"
	SIGQUIT Signal = "SIGQUIT"
	SIGUSR1 Signal = "SIGUSR1"
	SIGUSR2 Signal = "SIGUSR2"
)

type StopOptions

type StopOptions struct {
	// Stop all running containers
	All bool
	// List of container IDs or names to stop, should be empty if All flag is true
	IDs []string
	// Seconds to wait before killing the containers (default: 5)
	Time uint
	// Signal to send to the containers (default: SIGTERM)
	Signal Signal
}

StopOptions defines the options for stopping containers

type SystemClient

type SystemClient struct {
	// contains filtered or unexported fields
}

func NewSystemClient

func NewSystemClient(run func(ctx context.Context, args ...string) (string, error)) SystemClient

func (*SystemClient) Start

func (s *SystemClient) Start(ctx context.Context) (bool, error)

func (*SystemClient) Status

func (s *SystemClient) Status(ctx context.Context) (string, error)

func (*SystemClient) Stop

func (s *SystemClient) Stop(ctx context.Context) (bool, error)

type VolumeClient

type VolumeClient struct {
	// contains filtered or unexported fields
}

VolumeClient manages named volumes through the container CLI.

func NewVolumeClient

func NewVolumeClient(run func(ctx context.Context, args ...string) (string, error)) VolumeClient

NewVolumeClient creates a VolumeClient using the supplied command runner.

func (*VolumeClient) Create

func (v *VolumeClient) Create(ctx context.Context, opts VolumeCreateOptions) (string, error)

Create creates a named volume and returns the CLI output.

func (*VolumeClient) Delete

func (v *VolumeClient) Delete(ctx context.Context, opts VolumeDeleteOptions) (string, error)

Delete deletes the requested named volumes.

func (*VolumeClient) Exists

func (v *VolumeClient) Exists(ctx context.Context, name string) (bool, error)

Exists reports whether a named volume exists.

func (*VolumeClient) Inspect

func (v *VolumeClient) Inspect(ctx context.Context, names []string) (string, error)

Inspect returns the JSON description of the named volumes.

func (*VolumeClient) InspectSummary

func (v *VolumeClient) InspectSummary(ctx context.Context, name string) (VolumeSummary, bool, error)

InspectSummary returns metadata for a named volume and whether it exists.

func (*VolumeClient) List

func (v *VolumeClient) List(ctx context.Context) (string, error)

List returns the JSON description of all named volumes.

func (*VolumeClient) ListSummaries

func (v *VolumeClient) ListSummaries(ctx context.Context) ([]VolumeSummary, error)

ListSummaries returns names and labels for every named volume.

func (*VolumeClient) Prune

func (v *VolumeClient) Prune(ctx context.Context) (string, error)

Prune removes all volumes without container references.

type VolumeCreateOptions

type VolumeCreateOptions struct {
	// Name is the name of the volume to create.
	Name string
	// Labels associates metadata with the volume as KEY=VALUE pairs.
	Labels map[string]string
	// Options contains volume driver options as KEY=VALUE pairs.
	Options map[string]string
}

VolumeCreateOptions configures named-volume creation.

type VolumeDeleteOptions

type VolumeDeleteOptions struct {
	// Names identifies the volumes to delete. It must be empty when All is set.
	Names []string
	// All deletes every volume.
	All bool
}

VolumeDeleteOptions configures named-volume deletion.

type VolumeSummary

type VolumeSummary struct {
	Name   string
	Labels map[string]string
}

VolumeSummary is the metadata returned by volume list operations.

Jump to

Keyboard shortcuts

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