docker

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package docker is a thin, READ-ONLY Docker Engine API client that talks to the loopback docker-socket-proxy (plan §3) — never the raw socket. It deliberately implements only the handful of read endpoints the proxy's verb allowlist permits (CONTAINERS/INFO/VERSION), instead of pulling in the full Docker Go SDK: that keeps the binary near the ~12–18 MB footprint target (plan §2) and the dependency/supply-chain surface minimal (plan §15). Container-supplied fields (names, labels, image) are untrusted input and must be output-encoded by callers (html/template does this).

Index

Constants

View Source
const (
	LabelProject     = "com.docker.compose.project"
	LabelService     = "com.docker.compose.service"
	LabelWorkingDir  = "com.docker.compose.project.working_dir"
	LabelConfigFiles = "com.docker.compose.project.config_files"
)

Compose label keys Mooring groups containers by (an app = one project) and targets `docker compose` with (the project dir + config files).

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a read-only Engine API client over the loopback socket-proxy.

func New

func New(proxyAddr string) *Client

New returns a client targeting a loopback proxy address (host:port). The caller (config validation) guarantees the address is loopback.

func (*Client) Info

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

Info returns daemon-level info (container counts, ncpu, mem).

func (*Client) InspectContainer

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

InspectContainer returns the detailed state of one container.

func (*Client) ListContainers

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

ListContainers lists containers. all=true includes stopped ones.

func (*Client) StatsOneShot

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

StatsOneShot returns a single, immediate stats sample (no daemon-side double read). CPU% is derived by the caller from deltas between successive samples.

func (*Client) StreamLogs

func (c *Client) StreamLogs(ctx context.Context, id string, tail int, follow bool, onLine func(string)) error

StreamLogs follows a container's logs through the read-only socket-proxy (read plane — no docker child, no semaphore), demultiplexing the stdcopy framing and invoking onLine per (truncated) line until ctx is done or the stream ends.

func (*Client) Version

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

Version reports the daemon version (also a liveness probe).

type Container

type Container struct {
	ID              string                  `json:"Id"`
	Names           []string                `json:"Names"`
	Image           string                  `json:"Image"`
	State           string                  `json:"State"`  // running|exited|created|paused|...
	Status          string                  `json:"Status"` // human string, e.g. "Up 3 hours (healthy)"
	Labels          map[string]string       `json:"Labels"`
	NetworkSettings containerNetworkSummary `json:"NetworkSettings"`
}

Container is the subset of a GET /containers/json entry we use.

func (Container) ConfigFiles

func (c Container) ConfigFiles() []string

ConfigFiles returns the compose config file paths for the project, dropping empty/whitespace entries (a stray "" would become `docker compose -f ""`).

func (Container) IPs

func (c Container) IPs() []string

IPs returns the container's non-empty per-network IPv4 addresses, sorted by network name for a deterministic order (so a re-render of the same replica set produces byte-identical config and skips a needless Caddy reload).

func (Container) Name

func (c Container) Name() string

Name returns the primary container name without the leading slash.

func (Container) Project

func (c Container) Project() string

Project returns the compose project label (the app key), or "" if unlabeled.

func (Container) Service

func (c Container) Service() string

Service returns the compose service label, or "" if unlabeled.

func (Container) WorkingDir

func (c Container) WorkingDir() string

WorkingDir returns the compose project working directory (the app run_dir).

type ContainerInspect

type ContainerInspect struct {
	ID           string `json:"Id"`
	Name         string `json:"Name"`
	RestartCount int    `json:"RestartCount"`
	State        struct {
		Status     string `json:"Status"`
		Running    bool   `json:"Running"`
		ExitCode   int    `json:"ExitCode"`
		OOMKilled  bool   `json:"OOMKilled"`
		StartedAt  string `json:"StartedAt"`
		FinishedAt string `json:"FinishedAt"`
		Health     *struct {
			Status        string `json:"Status"` // healthy|unhealthy|starting
			FailingStreak int    `json:"FailingStreak"`
		} `json:"Health"`
	} `json:"State"`
	Config struct {
		Image  string            `json:"Image"`
		Labels map[string]string `json:"Labels"`
	} `json:"Config"`
	Mounts []struct {
		Type string `json:"Type"` // bind | volume | tmpfs
		Name string `json:"Name"` // volume name ("" for binds; a 64-hex hash for anonymous volumes)
		RW   bool   `json:"RW"`
	} `json:"Mounts"`
}

ContainerInspect is the subset of GET /containers/{id}/json we use.

func (ContainerInspect) HasSharedRWVolume

func (ci ContainerInspect) HasSharedRWVolume() bool

HasSharedRWVolume reports whether the container has a read-write mount that would be SHARED across replicas (a host bind or a named volume) — the auto-scaling C3 disqualifier. Anonymous volumes (a 64-hex name) are per-replica scratch and don't count; tmpfs never counts.

func (ContainerInspect) HealthStatus

func (ci ContainerInspect) HealthStatus() string

HealthStatus returns the container's healthcheck status, or "none" if it has no healthcheck.

type Info

type Info struct {
	Containers        int    `json:"Containers"`
	ContainersRunning int    `json:"ContainersRunning"`
	ContainersStopped int    `json:"ContainersStopped"`
	Images            int    `json:"Images"`
	NCPU              int    `json:"NCPU"`
	MemTotal          int64  `json:"MemTotal"`
	ServerVersion     string `json:"ServerVersion"`
}

Info is the subset of GET /info we use.

type Stats

type Stats struct {
	CPUStats    cpuStats `json:"cpu_stats"`
	PreCPUStats cpuStats `json:"precpu_stats"`
	MemoryStats struct {
		Usage uint64            `json:"usage"`
		Limit uint64            `json:"limit"`
		Stats map[string]uint64 `json:"stats"`
	} `json:"memory_stats"`
}

Stats is the subset of GET /containers/{id}/stats we use (raw counters; CPU% is computed from deltas between successive one-shot samples).

func (Stats) CPUPercentBetween

func (s Stats) CPUPercentBetween(prevTotal, prevSystem uint64) float64

CPUPercentBetween computes instantaneous CPU% from this sample's counters versus a previous sample's counters (Docker's formula). prevTotal/prevSystem are the previous one-shot's cpu_usage.total_usage / system_cpu_usage. Returns 0 when no meaningful delta exists yet (first sample).

func (Stats) MemLimit

func (s Stats) MemLimit() uint64

MemLimit returns the container memory limit.

func (Stats) MemUsed

func (s Stats) MemUsed() uint64

MemUsed returns memory usage minus reclaimable page cache (cgroup v2 inactive_file, or v1 cache), matching `docker stats`' notion of used memory.

func (Stats) RawCPU

func (s Stats) RawCPU() (total, system uint64)

RawCPU returns the raw counters used as the "previous" sample next tick.

type Version

type Version struct {
	Version    string `json:"Version"`
	APIVersion string `json:"ApiVersion"`
}

Version is the subset of GET /version we use.

Jump to

Keyboard shortcuts

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