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
- func DemuxStream(r io.Reader, stdout, stderr io.Writer) error
- func SocketPath() (string, error)
- type Client
- func (c *Client) ConnectNetwork(ctx context.Context, networkID, containerID string, aliases []string) error
- func (c *Client) CreateContainer(ctx context.Context, spec ContainerSpec) (string, error)
- func (c *Client) EnsureImage(ctx context.Context, image string, out io.Writer) error
- func (c *Client) EnsureNetwork(ctx context.Context, name string, labels map[string]string) (string, error)
- func (c *Client) ExecCreate(ctx context.Context, containerID string, spec ExecSpec) (string, error)
- func (c *Client) ExecInspect(ctx context.Context, execID string) (exitCode int, running bool, err error)
- func (c *Client) ExecResize(ctx context.Context, execID string, cols, rows uint16) error
- func (c *Client) ExecStart(ctx context.Context, execID string, tty bool) (net.Conn, *bufio.Reader, error)
- func (c *Client) FindNetworkByName(ctx context.Context, name string) (string, bool, error)
- func (c *Client) ListContainersByLabel(ctx context.Context, label, value string) ([]ContainerSummary, error)
- func (c *Client) ListNetworksByLabel(ctx context.Context, label, value string) ([]NetworkSummary, error)
- func (c *Client) RemoveContainer(ctx context.Context, id string, force bool) error
- func (c *Client) RemoveContainerByName(ctx context.Context, name string)
- func (c *Client) RemoveNetwork(ctx context.Context, id string) error
- func (c *Client) Rootless() bool
- func (c *Client) RunStep(ctx context.Context, step Step, out io.Writer) error
- func (c *Client) SampleStats(ctx context.Context, id string) (ContainerStats, error)
- func (c *Client) StartContainer(ctx context.Context, id string) error
- func (c *Client) StopContainer(ctx context.Context, id string, timeoutSeconds int) error
- func (c *Client) StreamLogs(ctx context.Context, id string, out io.Writer) error
- func (c *Client) WaitContainer(ctx context.Context, id string) (int, error)
- type ContainerSpec
- type ContainerStats
- type ContainerSummary
- type ExecSpec
- type NetworkSummary
- type Step
Constants ¶
const ManagedLabel = "local-preview.managed"
ManagedLabel marks every container and network this server creates.
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
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 ¶
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 (*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
CreateContainer creates (but does not start) a container.
func (*Client) EnsureImage ¶
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
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
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
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
RemoveContainer deletes a container.
func (*Client) RemoveContainerByName ¶ added in v0.1.5
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
RemoveNetwork deletes a network (its containers must be gone first).
func (*Client) Rootless ¶
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 ¶
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
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
StartContainer starts a created container.
func (*Client) StopContainer ¶ added in v0.1.5
StopContainer gracefully stops a container: the daemon sends SIGTERM, waits timeoutSeconds, then SIGKILLs.
func (*Client) StreamLogs ¶ added in v0.1.5
StreamLogs follows the container's combined output into out until the container exits or ctx is cancelled.
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
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
NetworkSummary is one row of a network listing.