dockerapi

package
v0.31.5 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: GPL-3.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dockerapi is a deliberately tiny Docker Engine API client — plain HTTP over the unix socket, no SDK dependency — sufficient to run one-shot build-step containers: ensure an image, create/start a container, stream its output, wait, remove. Used for manifest-declared build images so standalone local-preview gets reproducible builds even when its own host (possibly a container itself) has no toolchains.

Index

Constants

View Source
const ManagedLabel = "local-preview.managed"

ManagedLabel marks every container and network this server creates.

View Source
const StopTimeout = 5 * time.Second

StopTimeout is the graceful-stop grace period handed to the daemon.

Variables

This section is empty.

Functions

func DemuxStream added in v0.31.1

func DemuxStream(r io.Reader, stdout, stderr io.Writer) error

DemuxStream unpacks the engine's multiplexed attach stream (the non-TTY exec output format; same framing as container logs) into separate stdout and stderr writers, until EOF.

func SocketPath

func SocketPath() (string, error)

SocketPath resolves the daemon socket: $DOCKER_HOST (unix:// only), then /var/run/docker.sock, then the rootless per-user socket.

Types

type Client

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

Client talks to one Docker daemon.

func Connect

func Connect(ctx context.Context) (*Client, error)

Connect dials the daemon, verifies it responds, and probes rootless mode.

func (*Client) ConnectNetwork added in v0.1.5

func (c *Client) ConnectNetwork(ctx context.Context, networkID, containerID string, aliases []string) error

ConnectNetwork attaches a created container to an additional network.

func (*Client) CreateContainer added in v0.1.5

func (c *Client) CreateContainer(ctx context.Context, spec ContainerSpec) (string, error)

CreateContainer creates (but does not start) a container.

func (*Client) EnsureImage

func (c *Client) EnsureImage(ctx context.Context, image string, out io.Writer) error

EnsureImage makes the image locally available, pulling if needed.

func (*Client) EnsureNetwork added in v0.1.5

func (c *Client) EnsureNetwork(ctx context.Context, name string, labels map[string]string) (string, error)

EnsureNetwork returns the ID of the named bridge network, creating it (labeled) if absent.

func (*Client) ExecCreate added in v0.31.1

func (c *Client) ExecCreate(ctx context.Context, containerID string, spec ExecSpec) (string, error)

ExecCreate registers an exec instance in the container and returns its id.

func (*Client) ExecInspect added in v0.31.1

func (c *Client) ExecInspect(ctx context.Context, execID string) (exitCode int, running bool, err error)

ExecInspect reports whether the exec is still running and, once it isn't, its exit code.

func (*Client) ExecResize added in v0.31.1

func (c *Client) ExecResize(ctx context.Context, execID string, cols, rows uint16) error

ExecResize resizes the exec's TTY.

func (*Client) ExecStart added in v0.31.1

func (c *Client) ExecStart(ctx context.Context, execID string, tty bool) (net.Conn, *bufio.Reader, error)

ExecStart starts the exec instance attached, returning the hijacked bidirectional stream: write to conn for stdin, read output from br (which may already hold buffered stream bytes — always read through it, never conn directly). tty must match the ExecCreate spec. The caller owns conn.

func (*Client) FindNetworkByName added in v0.1.5

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

FindNetworkByName resolves a network name to its ID.

func (*Client) ListContainersByLabel added in v0.1.5

func (c *Client) ListContainersByLabel(ctx context.Context, label, value string) ([]ContainerSummary, error)

ListContainersByLabel returns containers (including stopped ones) carrying the label, optionally filtered to an exact value.

func (*Client) ListNetworksByLabel added in v0.1.5

func (c *Client) ListNetworksByLabel(ctx context.Context, label, value string) ([]NetworkSummary, error)

ListNetworksByLabel returns networks carrying the label, optionally filtered to an exact value.

func (*Client) RemoveContainer added in v0.1.5

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

RemoveContainer deletes a container.

func (*Client) RemoveContainerByName added in v0.1.5

func (c *Client) RemoveContainerByName(ctx context.Context, name string)

RemoveContainerByName force-removes a container by name, ignoring not-found. Used to clear a stale namesake before re-creating.

func (*Client) RemoveNetwork added in v0.1.5

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

RemoveNetwork deletes a network (its containers must be gone first).

func (*Client) Rootless

func (c *Client) Rootless() bool

Rootless reports whether the daemon runs rootless. Under rootless docker, container root maps to the daemon's host user — the right identity for writing bind-mounted build dirs.

func (*Client) RunStep

func (c *Client) RunStep(ctx context.Context, step Step, out io.Writer) error

RunStep creates, runs, and removes a container for the step, streaming combined output to out. Non-zero exits are errors.

func (*Client) SampleStats added in v0.1.7

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

SampleStats takes one stats sample of a running container. one-shot mode skips the daemon's ~1s priming read, so calls return immediately.

func (*Client) StartContainer added in v0.1.5

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

StartContainer starts a created container.

func (*Client) StopContainer added in v0.1.5

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

StopContainer gracefully stops a container: the daemon sends SIGTERM, waits timeoutSeconds, then SIGKILLs.

func (*Client) StreamLogs added in v0.1.5

func (c *Client) StreamLogs(ctx context.Context, id string, out io.Writer) error

StreamLogs follows the container's combined output into out until the container exits or ctx is cancelled.

func (*Client) WaitContainer added in v0.1.5

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

WaitContainer blocks until the container exits and returns its status code — the container analogue of exec.Cmd.Wait.

type ContainerSpec added in v0.1.5

type ContainerSpec struct {
	Image   string
	Cmd     []string // full argv; overrides the image entrypoint
	Env     []string
	User    string
	WorkDir string
	Binds   []string
	Labels  map[string]string
	Name    string
	Port    int
	// PublishIP, when set, additionally publishes Port on that host address —
	// how a worker exposes its processes to the control node's proxy. The
	// loopback binding always exists (local health checks poll it); firewalls
	// own who can reach the extra one.
	PublishIP string
	// NetworkID, when set, attaches the container at create time with
	// NetworkAlias as its DNS name on that network.
	NetworkID    string
	NetworkAlias string
}

ContainerSpec describes one supervised preview process container. Port is published to 127.0.0.1:<Port> on the daemon host — the process must bind 0.0.0.0:<Port> inside the container.

type ContainerStats added in v0.1.7

type ContainerStats struct {
	CPUTotal       uint64 // container CPU consumed, nanoseconds
	SystemCPUTotal uint64 // host CPU across all cores, nanoseconds
	OnlineCPUs     int    // 0 when the daemon doesn't report it
	// MemoryBytes is usage minus reclaimable file cache — the figure
	// `docker stats` shows.
	MemoryBytes      uint64
	MemoryLimitBytes uint64 // cgroup limit; the host total when unlimited
}

ContainerStats is one resource sample of a running container, reduced to what the observability endpoints report. CPU fields are cumulative counters: a percentage needs two samples — Δ(CPUTotal)/Δ(SystemCPUTotal) × OnlineCPUs × 100.

type ContainerSummary added in v0.1.5

type ContainerSummary struct {
	ID     string            `json:"Id"`
	Labels map[string]string `json:"Labels"`
}

ContainerSummary is one row of a container listing.

type ExecSpec added in v0.31.1

type ExecSpec struct {
	Cmd []string
	Env []string
	// TTY allocates a pseudo-terminal: output arrives as one raw stream and
	// stderr is folded into it. Without it, output is multiplexed and
	// DemuxStream splits the two.
	TTY bool
	// Stdin attaches the caller's input stream.
	Stdin bool
	// WorkDir and User default to the container's own when empty.
	WorkDir string
	User    string
}

ExecSpec configures one exec in a running container.

type NetworkSummary added in v0.1.5

type NetworkSummary struct {
	ID   string `json:"Id"`
	Name string `json:"Name"`
}

NetworkSummary is one row of a network listing.

type Step

type Step struct {
	Image    string
	Cmd      []string
	WorkDir  string
	Env      []string
	User     string
	Binds    []string
	Networks []string
}

Step is one command run in a fresh container to completion. Networks names existing external networks the container joins before starting.

Jump to

Keyboard shortcuts

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