Documentation
¶
Overview ¶
Package docker wraps the Docker SDK behind havn-native types.
Index ¶
- Variables
- func BuildMounts(binds []BindMount, volumes []VolumeMount) []dockermount.Mount
- func BuildPortBindings(ports []string) (nat.PortSet, nat.PortMap, error)
- func EnvSlice(env map[string]string) []string
- func ParseMemoryBytes(s string) int64
- func TerminalFd(r io.Reader) int
- type AttachOpts
- type BindMount
- type BuildOpts
- type Client
- func (c *Client) ContainerAttach(ctx context.Context, nameOrID string, opts AttachOpts) (int, error)
- func (c *Client) ContainerCreate(ctx context.Context, opts CreateOpts) (string, error)
- func (c *Client) ContainerExec(ctx context.Context, nameOrID string, opts ExecOpts) (ExecResult, error)
- func (c *Client) ContainerInspect(ctx context.Context, nameOrID string) (ContainerInfo, error)
- func (c *Client) ContainerList(ctx context.Context, filters ContainerListFilters) ([]ContainerInfo, error)
- func (c *Client) ContainerRemove(ctx context.Context, nameOrID string, opts RemoveOpts) error
- func (c *Client) ContainerStart(ctx context.Context, nameOrID string) error
- func (c *Client) ContainerStop(ctx context.Context, nameOrID string, opts StopOpts) error
- func (c *Client) CopyFromContainer(ctx context.Context, nameOrID string, srcPath string) (io.ReadCloser, error)
- func (c *Client) CopyToContainer(ctx context.Context, nameOrID string, dstPath string, tarStream io.Reader) error
- func (c *Client) ImageBuild(ctx context.Context, opts BuildOpts) error
- func (c *Client) ImageExists(ctx context.Context, name string) (bool, error)
- func (c *Client) ImageInspect(ctx context.Context, name string) (ImageInfo, error)
- func (c *Client) ImagePull(ctx context.Context, ref string, output io.Writer) error
- func (c *Client) Info(ctx context.Context) (DaemonInfo, error)
- func (c *Client) NetworkCreate(ctx context.Context, opts NetworkCreateOpts) error
- func (c *Client) NetworkInspect(ctx context.Context, name string) (NetworkInfo, error)
- func (c *Client) NetworkList(ctx context.Context, filters NetworkListFilters) ([]NetworkInfo, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) SetLogger(logger *slog.Logger)
- func (c *Client) VolumeCreate(ctx context.Context, opts VolumeCreateOpts) error
- func (c *Client) VolumeInspect(ctx context.Context, name string) (VolumeInfo, error)
- func (c *Client) VolumeList(ctx context.Context, filters VolumeListFilters) ([]VolumeInfo, error)
- type ConnectedContainer
- type ContainerInfo
- type ContainerListFilters
- type ContainerNotFoundError
- type CreateOpts
- type DaemonInfo
- type DaemonUnreachableError
- type ExecOpts
- type ExecResult
- type ImageBuildError
- type ImageInfo
- type ImageNotFoundError
- type MountInfo
- type NetworkCreateOpts
- type NetworkInfo
- type NetworkListFilters
- type NetworkNotFoundError
- type RemoveOpts
- type StopOpts
- type VolumeCreateOpts
- type VolumeInfo
- type VolumeListFilters
- type VolumeMount
- type VolumeNotFoundError
Constants ¶
This section is empty.
Variables ¶
var ErrNetworkAlreadyExists = errors.New("network already exists")
ErrNetworkAlreadyExists is returned by NetworkCreate when the requested network name is already in use.
Functions ¶
func BuildMounts ¶
func BuildMounts(binds []BindMount, volumes []VolumeMount) []dockermount.Mount
BuildMounts converts havn bind and volume mounts to Docker SDK mounts.
func BuildPortBindings ¶
BuildPortBindings converts host:container(/proto) mappings into Docker types.
func EnvSlice ¶
EnvSlice converts a map of environment variables to the Docker SDK's "KEY=VALUE" slice format.
func ParseMemoryBytes ¶
ParseMemoryBytes converts a memory string like "4g" or "512m" to bytes. Returns 0 for empty or unrecognized strings.
func TerminalFd ¶
TerminalFd returns the file descriptor if r provides one via an Fd() method (e.g. *os.File), or -1 if the reader is not backed by a file descriptor.
Types ¶
type AttachOpts ¶
type AttachOpts struct {
Cmd []string // Command and arguments to execute
Env []string // Environment variables (KEY=VALUE)
Workdir string // Working directory inside the container
User string // User to run the command as
Stdin io.Reader // Host stdin (typically os.Stdin)
Stdout io.Writer // Host stdout (typically os.Stdout)
Stderr io.Writer // Host stderr (typically os.Stderr)
}
AttachOpts holds parameters for an interactive tty exec session.
type BuildOpts ¶
type BuildOpts struct {
Tag string // image tag to apply
Context string // path to build context directory
Dockerfile string // Dockerfile path relative to context
BuildArgs map[string]string // build-time variables
Output io.Writer // streaming build output destination
}
BuildOpts holds parameters for building a Docker image.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the Docker SDK client. The SDK client is a private field, never exposed outside this package.
func NewClient ¶
NewClient creates a Client using environment-based configuration (DOCKER_HOST, DOCKER_API_VERSION, etc.). Construction is lazy — no connection is made until a method is called.
func NewClientWithHost ¶
NewClientWithHost creates a Client targeting a specific Docker host.
func NewClientWithHostAndLogger ¶
NewClientWithHostAndLogger creates a Client targeting a specific Docker host and uses the injected logger for structured diagnostics.
func (*Client) ContainerAttach ¶
func (c *Client) ContainerAttach(ctx context.Context, nameOrID string, opts AttachOpts) (int, error)
ContainerAttach creates and attaches to an interactive tty exec session in the specified container. It proxies stdin/stdout/stderr between the host and the container process, handles SIGWINCH for terminal resizing, and puts the host terminal into raw mode for the duration of the session. Returns the remote process exit code. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) ContainerCreate ¶
ContainerCreate creates a container from the given options. Returns the container ID on success. Returns *ImageNotFoundError if the image does not exist locally.
func (*Client) ContainerExec ¶
func (c *Client) ContainerExec(ctx context.Context, nameOrID string, opts ExecOpts) (ExecResult, error)
ContainerExec runs a non-interactive one-shot command in the specified container and captures stdout/stderr. A non-zero exit code is returned in ExecResult, not as an error — only exec-plumbing failures return errors. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) ContainerInspect ¶
ContainerInspect returns detailed information about a container by name or ID. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) ContainerList ¶
func (c *Client) ContainerList(ctx context.Context, filters ContainerListFilters) ([]ContainerInfo, error)
ContainerList returns containers matching the given filters. Returns an empty slice (not nil) when no containers match.
func (*Client) ContainerRemove ¶
ContainerRemove removes a container by name or ID. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) ContainerStart ¶
ContainerStart starts a container by name or ID. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) ContainerStop ¶
ContainerStop stops a container by name or ID with the given options. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) CopyFromContainer ¶
func (c *Client) CopyFromContainer(ctx context.Context, nameOrID string, srcPath string) (io.ReadCloser, error)
CopyFromContainer returns a tar stream of the contents at srcPath inside the container. The caller must close the returned ReadCloser. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) CopyToContainer ¶
func (c *Client) CopyToContainer(ctx context.Context, nameOrID string, dstPath string, tarStream io.Reader) error
CopyToContainer copies a tar stream into the container at dstPath. Returns *ContainerNotFoundError if the container does not exist.
func (*Client) ImageBuild ¶
ImageBuild builds a Docker image from the given build context. Build output is streamed to opts.Output. Returns *ImageBuildError if the build itself fails (Dockerfile error, etc.).
func (*Client) ImageExists ¶
ImageExists checks whether an image exists locally. Returns (false, nil) when the image is not found — not an error. Other failures (e.g., daemon unreachable) are returned as errors.
func (*Client) ImageInspect ¶
ImageInspect returns metadata about a Docker image by name or ID. Returns *ImageNotFoundError if the image does not exist locally.
func (*Client) ImagePull ¶
ImagePull pulls an image reference from the configured registry and streams daemon output to output when provided.
func (*Client) Info ¶
func (c *Client) Info(ctx context.Context) (DaemonInfo, error)
Info returns metadata about the Docker daemon. Returns *DaemonUnreachableError when the daemon is unreachable.
func (*Client) NetworkCreate ¶
func (c *Client) NetworkCreate(ctx context.Context, opts NetworkCreateOpts) error
NetworkCreate creates a named network with the given options. Returns ErrNetworkAlreadyExists if a network with the same name already exists.
func (*Client) NetworkInspect ¶
NetworkInspect returns information about a network by name. Returns *NetworkNotFoundError if the network does not exist.
func (*Client) NetworkList ¶
func (c *Client) NetworkList(ctx context.Context, filters NetworkListFilters) ([]NetworkInfo, error)
NetworkList returns networks matching the given filters. Returns an empty slice (not nil) when no networks match.
func (*Client) Ping ¶
Ping checks if the Docker daemon is reachable. Returns *DaemonUnreachableError when it is not; nil when it is.
func (*Client) VolumeCreate ¶
func (c *Client) VolumeCreate(ctx context.Context, opts VolumeCreateOpts) error
VolumeCreate creates a named volume with the given options.
func (*Client) VolumeInspect ¶
VolumeInspect returns information about a volume by name. Returns *VolumeNotFoundError if the volume does not exist.
func (*Client) VolumeList ¶
func (c *Client) VolumeList(ctx context.Context, filters VolumeListFilters) ([]VolumeInfo, error)
VolumeList returns volumes matching the given filters. Returns an empty slice (not nil) when no volumes match.
type ConnectedContainer ¶
ConnectedContainer identifies a container attached to a network.
type ContainerInfo ¶
type ContainerInfo struct {
ID string
Name string
Image string
Status string // e.g. "running", "exited"
Labels map[string]string
Mounts []MountInfo
Networks []string
Env []string
}
ContainerInfo holds read-only state of a container.
type ContainerListFilters ¶
type ContainerListFilters struct {
Labels map[string]string // label key=value pairs to match
NamePrefix string // container name prefix filter
Status string // e.g. "running", "exited"
}
ContainerListFilters holds filter criteria for listing containers.
type ContainerNotFoundError ¶
type ContainerNotFoundError struct {
Name string
}
ContainerNotFoundError indicates a container does not exist in Docker.
func (*ContainerNotFoundError) Error ¶
func (e *ContainerNotFoundError) Error() string
func (*ContainerNotFoundError) ErrorDetails ¶
func (e *ContainerNotFoundError) ErrorDetails() map[string]any
ErrorDetails returns structured fields for JSON error output.
func (*ContainerNotFoundError) ErrorType ¶
func (e *ContainerNotFoundError) ErrorType() string
ErrorType returns the stable snake_case identifier for this error.
type CreateOpts ¶
type CreateOpts struct {
Image string
Name string
Network string
Ports []string
Env map[string]string
Labels map[string]string
BindMounts []BindMount
VolumeMounts []VolumeMount
RestartPolicy string
TTY bool
Workdir string
Cmd []string
Entrypoint []string
User string
CPUs int
Memory string
MemorySwap string
AutoRemove bool
}
CreateOpts holds parameters for creating a container.
type DaemonInfo ¶
DaemonInfo holds metadata about the Docker daemon.
type DaemonUnreachableError ¶
type DaemonUnreachableError struct {
Host string
}
DaemonUnreachableError indicates the Docker daemon cannot be contacted.
func (*DaemonUnreachableError) Error ¶
func (e *DaemonUnreachableError) Error() string
func (*DaemonUnreachableError) ErrorDetails ¶
func (e *DaemonUnreachableError) ErrorDetails() map[string]any
ErrorDetails returns structured fields for JSON error output.
func (*DaemonUnreachableError) ErrorType ¶
func (e *DaemonUnreachableError) ErrorType() string
ErrorType returns the stable snake_case identifier for this error.
type ExecOpts ¶
type ExecOpts struct {
Cmd []string // Command and arguments to execute
Env []string // Environment variables (KEY=VALUE)
Workdir string // Working directory inside the container
User string // User to run the command as
OnStderrLine func(string) // Optional callback for incremental stderr lines
}
ExecOpts holds parameters for a non-interactive one-shot exec.
type ExecResult ¶
type ExecResult struct {
ExitCode int // Process exit code
Stdout []byte // Captured stdout bytes
Stderr []byte // Captured stderr bytes
}
ExecResult holds the outcome of a non-interactive exec. A non-zero ExitCode is not an error — only exec-plumbing failures return errors.
type ImageBuildError ¶
ImageBuildError indicates that a Docker image build failed.
func (*ImageBuildError) Error ¶
func (e *ImageBuildError) Error() string
func (*ImageBuildError) ErrorDetails ¶
func (e *ImageBuildError) ErrorDetails() map[string]any
ErrorDetails returns structured fields for JSON error output.
func (*ImageBuildError) ErrorType ¶
func (e *ImageBuildError) ErrorType() string
ErrorType returns the stable snake_case identifier for this error.
type ImageNotFoundError ¶
type ImageNotFoundError struct {
Name string
}
ImageNotFoundError indicates a Docker image does not exist locally.
func (*ImageNotFoundError) Error ¶
func (e *ImageNotFoundError) Error() string
func (*ImageNotFoundError) ErrorDetails ¶
func (e *ImageNotFoundError) ErrorDetails() map[string]any
ErrorDetails returns structured fields for JSON error output.
func (*ImageNotFoundError) ErrorType ¶
func (e *ImageNotFoundError) ErrorType() string
ErrorType returns the stable snake_case identifier for this error.
type MountInfo ¶
type MountInfo struct {
Source string
Target string
Type string // e.g. "bind", "volume"
Mode string // e.g. "rw", "ro"
}
MountInfo describes a mount attached to a container.
type NetworkCreateOpts ¶
NetworkCreateOpts holds parameters for creating a network.
type NetworkInfo ¶
type NetworkInfo struct {
Name string
ID string
Driver string
ConnectedContainers []ConnectedContainer
}
NetworkInfo holds read-only state of a Docker network.
type NetworkListFilters ¶
type NetworkListFilters struct {
NamePrefix string // network name prefix filter
}
NetworkListFilters holds filter criteria for listing networks.
type NetworkNotFoundError ¶
type NetworkNotFoundError struct {
Name string
}
NetworkNotFoundError indicates a Docker network does not exist.
func (*NetworkNotFoundError) Error ¶
func (e *NetworkNotFoundError) Error() string
func (*NetworkNotFoundError) ErrorDetails ¶
func (e *NetworkNotFoundError) ErrorDetails() map[string]any
ErrorDetails returns structured fields for JSON error output.
func (*NetworkNotFoundError) ErrorType ¶
func (e *NetworkNotFoundError) ErrorType() string
ErrorType returns the stable snake_case identifier for this error.
type RemoveOpts ¶
RemoveOpts holds parameters for removing a container.
type StopOpts ¶
type StopOpts struct {
Timeout int // seconds
}
StopOpts holds parameters for stopping a container.
type VolumeCreateOpts ¶
VolumeCreateOpts holds parameters for creating a volume.
type VolumeInfo ¶
type VolumeInfo struct {
Name string
Driver string
Labels map[string]string
Mountpoint string
CreatedAt string
}
VolumeInfo holds read-only state of a Docker volume.
type VolumeListFilters ¶
type VolumeListFilters struct {
Labels map[string]string // label key=value pairs to match
NamePrefix string // volume name prefix filter
}
VolumeListFilters holds filter criteria for listing volumes.
type VolumeMount ¶
VolumeMount represents a named volume mount.
type VolumeNotFoundError ¶
type VolumeNotFoundError struct {
Name string
}
VolumeNotFoundError indicates a Docker volume does not exist.
func (*VolumeNotFoundError) Error ¶
func (e *VolumeNotFoundError) Error() string
func (*VolumeNotFoundError) ErrorDetails ¶
func (e *VolumeNotFoundError) ErrorDetails() map[string]any
ErrorDetails returns structured fields for JSON error output.
func (*VolumeNotFoundError) ErrorType ¶
func (e *VolumeNotFoundError) ErrorType() string
ErrorType returns the stable snake_case identifier for this error.