docker

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package docker talks to the Docker Engine API exposed by Colima's unix socket. The client is created lazily so the app starts even when the VM is down, and re-dials if the socket path changes.

Index

Constants

View Source
const ComposeProjectLabel = "com.docker.compose.project"

ComposeProjectLabel groups containers into compose stacks.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client wraps the Docker SDK with lazy, socket-aware connection management.

func New

func New() *Client

New returns an unconnected client. The connection is established on first use.

func (*Client) Close

func (c *Client) Close() error

Close releases the underlying connection.

func (*Client) ComposeUpFile

func (c *Client) ComposeUpFile(ctx context.Context, dir, file, name string, ownName bool) (<-chan string, <-chan error, error)

ComposeUpFile deploys a discovered compose project by file path — it has no containers yet, so it can't be found by label. --project-directory is set so an adjacent .env and relative paths resolve; -p is passed only when the file doesn't declare its own name, so the resulting labels match what we discovered.

func (*Client) ContainerExists

func (c *Client) ContainerExists(ctx context.Context, idOrName string) (bool, error)

ContainerExists reports whether a container with the given id or name exists.

func (*Client) EngineInfo

func (c *Client) EngineInfo(ctx context.Context) EngineInfo

EngineInfo pings the daemon via `docker info`. A nil/zero result (Reachable false) means the engine is down or unreachable — never an error to the caller.

func (*Client) ImageExists

func (c *Client) ImageExists(ctx context.Context, idOrRef string) (bool, error)

func (*Client) InspectContainer

func (c *Client) InspectContainer(ctx context.Context, id string) (ContainerDetail, error)

func (*Client) ListContainers

func (c *Client) ListContainers(ctx context.Context) ([]Container, error)

ListContainers returns all containers (running and stopped).

func (*Client) ListImages

func (c *Client) ListImages(ctx context.Context) ([]Image, error)

func (*Client) ListNetworks

func (c *Client) ListNetworks(ctx context.Context) ([]Network, error)

func (*Client) ListStacks

func (c *Client) ListStacks(ctx context.Context) ([]Stack, error)

ListStacks groups all containers by their compose project label.

func (*Client) ListVolumes

func (c *Client) ListVolumes(ctx context.Context) ([]Volume, error)

func (*Client) NetworkExists

func (c *Client) NetworkExists(ctx context.Context, idOrName string) (bool, error)

func (*Client) PruneImages

func (c *Client) PruneImages(ctx context.Context) (int, int64, error)

PruneImages removes dangling images and reports what was reclaimed.

func (*Client) PruneVolumes

func (c *Client) PruneVolumes(ctx context.Context) (int, int64, error)

func (*Client) PruneableVolumes

func (c *Client) PruneableVolumes(ctx context.Context) ([]VolumePreview, error)

PruneableVolumes lists volumes no container references — exactly what a prune would remove — with sizes from the disk-usage API (0 when a driver can't report it, so the prune still works, the total is just approximate).

func (*Client) PullImage

func (c *Client) PullImage(ctx context.Context, ref string, emit func(map[string]any)) error

PullImage pulls ref and invokes emit for each progress object in the stream.

func (*Client) RemoveContainer

func (c *Client) RemoveContainer(ctx context.Context, id string, force bool) error

func (*Client) RemoveImage

func (c *Client) RemoveImage(ctx context.Context, id string, force bool) error

func (*Client) RemoveNetwork

func (c *Client) RemoveNetwork(ctx context.Context, id string) error

func (*Client) RemoveVolume

func (c *Client) RemoveVolume(ctx context.Context, name string, force bool) error

func (*Client) RestartContainer

func (c *Client) RestartContainer(ctx context.Context, id string) error

func (*Client) SearchImages

func (c *Client) SearchImages(ctx context.Context, term string, limit int) ([]SearchResult, error)

SearchImages queries the registry (Docker Hub) through the daemon, so it uses the same network path as a pull and needs no extra HTTP client or CORS dance.

func (*Client) StartContainer

func (c *Client) StartContainer(ctx context.Context, id string) error

func (*Client) StopContainer

func (c *Client) StopContainer(ctx context.Context, id string) error

func (*Client) StreamCompose

func (c *Client) StreamCompose(ctx context.Context, project, action string) (<-chan string, <-chan error, error)

StreamCompose runs a compose action against a discovered stack, streaming output. The stack's config-files and working-dir labels are replayed so compose targets exactly the original project definition.

func (*Client) StreamEvents

func (c *Client) StreamEvents(ctx context.Context) (<-chan Event, <-chan error, error)

StreamEvents emits relevant docker events until ctx is cancelled. The error channel yields a terminal error (e.g. the daemon going away).

func (*Client) StreamLogs

func (c *Client) StreamLogs(ctx context.Context, id string, tail int, follow bool, until string, emit func(stream, ts, line string)) error

StreamLogs reads a container's logs, invoking emit per line. With follow=true it tails live until ctx is cancelled; with follow=false it returns one historical batch and stops. `until` (RFC3339Nano) bounds the batch to lines before that moment — the cursor for lazy-loading older lines. Timestamps are always on so each line carries the cursor; the ts is split out of the displayed text.

func (*Client) SystemPrune

func (c *Client) SystemPrune(ctx context.Context, opts PruneOptions, emit func(string)) (PruneResult, error)

SystemPrune runs the selected prune steps (stopped containers, unused networks, dangling images, build cache, unused volumes). Build cache prunes dangling layers by default; BuildCacheAll (All:true) also removes cache for existing images — the larger amount SystemUsage previews. emit (nil-safe) gets a line per step.

func (*Client) SystemUsage

func (c *Client) SystemUsage(ctx context.Context) (SystemUsage, error)

func (*Client) TagImage

func (c *Client) TagImage(ctx context.Context, id, ref string) error

TagImage adds a repository:tag reference to an existing image, so a digest-pinned (untagged) image gains a readable name.

func (*Client) VolumeExists

func (c *Client) VolumeExists(ctx context.Context, name string) (bool, error)

type Container

type Container struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Image   string `json:"image"`
	ImageID string `json:"imageId"` // digest, for matching a container to its image
	State   string `json:"state"`
	Status  string `json:"status"`
	Created int64  `json:"created"`
	Ports   []Port `json:"ports"`
	Project string `json:"project"`
}

Container is the frontend-facing DTO; SDK types are not leaked past this package so the API contract stays stable across SDK upgrades.

type ContainerDetail

type ContainerDetail struct {
	ID            string            `json:"id"`
	Name          string            `json:"name"`
	Image         string            `json:"image"`
	ImageID       string            `json:"imageId"`
	Created       string            `json:"created"`
	State         string            `json:"state"`
	Running       bool              `json:"running"`
	StartedAt     string            `json:"startedAt"`
	ExitCode      int               `json:"exitCode"`
	Health        string            `json:"health"`
	RestartPolicy string            `json:"restartPolicy"`
	Command       string            `json:"command"`
	WorkingDir    string            `json:"workingDir"`
	Env           []string          `json:"env"`
	Labels        map[string]string `json:"labels"`
	Mounts        []MountInfo       `json:"mounts"`
	Networks      []NetInfo         `json:"networks"`
}

ContainerDetail is the curated inspect payload the UI shows in a detail panel.

type EngineInfo

type EngineInfo struct {
	Reachable     bool
	Host          string
	NCPU          int
	MemTotal      int64
	Architecture  string
	ServerVersion string
	Driver        string
	OS            string
}

EngineInfo is a generic snapshot of the Docker daemon, used to report status when there's no colima VM to describe.

type Event

type Event struct {
	Type   string `json:"type"`
	Action string `json:"action"`
	ID     string `json:"id"`
	Name   string `json:"name"`
}

Event is a trimmed docker event used to trigger live UI refreshes.

type Image

type Image struct {
	ID         string   `json:"id"`
	Tags       []string `json:"tags"`
	Size       int64    `json:"size"`
	Created    int64    `json:"created"`
	Containers int64    `json:"containers"`
}

type MountInfo

type MountInfo struct {
	Type        string `json:"type"`
	Source      string `json:"source"`
	Destination string `json:"destination"`
	Name        string `json:"name"`
	RW          bool   `json:"rw"`
}

type NetInfo

type NetInfo struct {
	Name       string `json:"name"`
	IPAddress  string `json:"ipAddress"`
	Gateway    string `json:"gateway"`
	MacAddress string `json:"macAddress"`
}

type Network

type Network struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Driver   string `json:"driver"`
	Scope    string `json:"scope"`
	Internal bool   `json:"internal"`
	Created  int64  `json:"created"`
}

type Port

type Port struct {
	Private uint16 `json:"private"`
	Public  uint16 `json:"public"`
	Type    string `json:"type"`
}

type PruneOptions

type PruneOptions struct {
	Containers    bool
	Images        bool
	Networks      bool
	BuildCache    bool
	BuildCacheAll bool
	Volumes       bool
}

PruneOptions selects which categories a system prune removes. Each maps to one `docker … prune` step; nothing runs unless explicitly selected. BuildCacheAll extends the build-cache step from dangling-only to every unused entry.

type PruneResult

type PruneResult struct {
	Containers int   `json:"containers"`
	Images     int   `json:"images"`
	Networks   int   `json:"networks"`
	Volumes    int   `json:"volumes"`
	Reclaimed  int64 `json:"reclaimed"`
}

PruneResult reports what a system prune removed.

type Sampler

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

Sampler computes container CPU% by diffing successive one-shot readings, avoiding a persistent stats stream per container. One Sampler backs the single shared /api/stats SSE channel.

func NewSampler

func NewSampler(c *Client) *Sampler

func (*Sampler) Sample

func (s *Sampler) Sample(ctx context.Context) ([]Stat, error)

Sample reads stats for all running containers once and returns the deltas. The first reading for a container reports CPU 0 (two samples are needed).

type SearchResult

type SearchResult struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Stars       int    `json:"stars"`
	Official    bool   `json:"official"`
}

SearchResult is one Docker Hub search hit.

type Stack

type Stack struct {
	Name        string      `json:"name"`
	Running     int         `json:"running"`
	Total       int         `json:"total"`
	ConfigFiles string      `json:"configFiles"`
	WorkingDir  string      `json:"workingDir"`
	Containers  []Container `json:"containers"`
}

Stack is a discovered compose project (grouped by the project label).

type Stat

type Stat struct {
	ID       string  `json:"id"`
	CPU      float64 `json:"cpu"` // percent of total host CPU
	Mem      int64   `json:"mem"`
	MemLimit int64   `json:"memLimit"`
}

Stat is a per-container resource sample for the frontend.

type SystemUsage

type SystemUsage struct {
	StoppedContainers int   `json:"stoppedContainers"`
	ContainersSize    int64 `json:"containersSize"`
	DanglingImages    int   `json:"danglingImages"`
	ImagesSize        int64 `json:"imagesSize"`
	BuildCacheSize    int64 `json:"buildCacheSize"`
	UnusedVolumes     int   `json:"unusedVolumes"`
	VolumesSize       int64 `json:"volumesSize"`
	Reclaimable       int64 `json:"reclaimable"` // excludes volumes (opt-in)
}

SystemUsage summarises what a `docker system prune` would reclaim. Counts and sizes mirror what the prune actually removes (stopped containers, dangling images, inactive build cache) plus unused volumes shown separately.

type Volume

type Volume struct {
	Name       string `json:"name"`
	Driver     string `json:"driver"`
	Mountpoint string `json:"mountpoint"`
	Scope      string `json:"scope"`
	CreatedAt  string `json:"createdAt"`
}

type VolumePreview

type VolumePreview struct {
	Name      string `json:"name"`
	Size      int64  `json:"size"`
	CreatedAt string `json:"createdAt"`
}

VolumePreview is one prune candidate: an unused volume and (best-effort) the space it would reclaim.

Jump to

Keyboard shortcuts

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