docker

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package docker provides a Docker integration layer for Gopherstack. It wraps the Docker SDK to support image pulling, container lifecycle management (create/start/stop/remove), volume mounts, and a simple warm-container pool with configurable idle timeout.

This package is the foundation for Lambda runtime container execution (Python, Node.js, Java, .NET, Ruby, custom images) planned for v0.7.

Index

Constants

This section is empty.

Variables

View Source
var ErrContainerNotFound = errors.New("container not found")

ErrContainerNotFound is returned when a requested container does not exist in the pool.

View Source
var ErrDockerUnavailable = errors.New("docker daemon is not available")

ErrDockerUnavailable is returned when Docker is not available on the host.

View Source
var ErrPoolExhausted = errors.New("container pool exhausted")

ErrPoolExhausted is returned when no warm container is available and the pool is full.

Functions

This section is empty.

Types

type APIClient

type APIClient interface {
	ImagePull(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error)
	ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
	ContainerCreate(
		ctx context.Context,
		cfg *container.Config,
		hostConfig *container.HostConfig,
		networkingConfig any,
		platform any,
		containerName string,
	) (container.CreateResponse, error)
	ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error
	ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error
	ContainerRemove(ctx context.Context, containerID string, options container.RemoveOptions) error
	Ping(ctx context.Context) (any, error)
	Close() error
}

APIClient is a subset of the Docker SDK client interface used by this package. It is defined as an interface to enable testing without a real Docker daemon.

type Client

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

Client is the Gopherstack Docker client. It wraps the Docker SDK and provides image management and a per-image warm container pool.

func NewClient

func NewClient(cfg Config) (*Client, error)

NewClient creates a new Docker Client using the host's Docker daemon. Returns ErrDockerUnavailable if Docker is not reachable.

func NewClientWithAPI

func NewClientWithAPI(api APIClient, cfg Config) *Client

NewClientWithAPI creates a Client with an injected APIClient. This is primarily intended for testing; production code should use NewClient.

func (*Client) AcquireWarm

func (c *Client) AcquireWarm(ctx context.Context, spec ContainerSpec) (*PooledContainer, error)

AcquireWarm returns a warm container from the pool for the given image. If no warm container is available, a new one is created (up to PoolSize). The caller must call ReleaseContainer when done.

Note: pool capacity is a soft limit. When multiple goroutines call AcquireWarm concurrently, the pool may temporarily exceed PoolSize by the number of concurrent callers that see no idle container and concurrently create new ones.

func (*Client) Close

func (c *Client) Close() error

Close releases resources held by the Docker client.

func (*Client) CreateAndStart

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

CreateAndStart creates a new container from spec and starts it. Returns the container ID.

func (*Client) HasImage

func (c *Client) HasImage(ctx context.Context, imageRef string) (bool, error)

HasImage reports whether the given image reference is already present locally.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks whether the Docker daemon is reachable.

func (*Client) PullImage

func (c *Client) PullImage(ctx context.Context, imageRef string) error

PullImage pulls the specified image from the registry. The pull output is discarded; callers that need progress should use the Docker SDK directly.

func (*Client) ReapIdleContainers

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

ReapIdleContainers stops and removes containers that have been idle longer than IdleTimeout. It is intended to be called periodically by a background goroutine.

func (*Client) ReleaseContainer

func (c *Client) ReleaseContainer(containerID string) error

ReleaseContainer marks a pooled container as idle. If the container is not in the pool, ErrContainerNotFound is returned.

func (*Client) StartReaper

func (c *Client) StartReaper(ctx context.Context, interval time.Duration)

StartReaper launches a background goroutine that periodically reaps idle containers. It stops when ctx is cancelled.

func (*Client) StopAndRemove

func (c *Client) StopAndRemove(ctx context.Context, containerID string) error

StopAndRemove stops and removes a container.

type Config

type Config struct {
	// Logger is an optional structured logger.
	Logger *slog.Logger
	// PoolSize is the maximum number of warm containers per image.
	// Defaults to 3.
	PoolSize int
	// IdleTimeout is the duration after which an idle container is reaped.
	// Defaults to 10 minutes.
	IdleTimeout time.Duration
}

Config holds configuration for the Docker integration layer.

type ContainerSpec

type ContainerSpec struct {
	// Image is the Docker image reference.
	Image string
	// Name is an optional container name.
	Name string
	// Env is a list of environment variables in KEY=VALUE format.
	Env []string
	// Mounts is a list of bind-mount strings in HOST:CONTAINER[:OPTIONS] format.
	Mounts []string
	// Cmd overrides the image's default CMD.
	Cmd []string
}

ContainerSpec holds the specification for creating a container.

type PooledContainer

type PooledContainer struct {
	// LastUsed is the timestamp of the last time this container was used.
	LastUsed time.Time
	// ID is the Docker container ID.
	ID string
	// Image is the image the container was started from.
	Image string
	// InUse indicates whether the container is currently handling an invocation.
	InUse bool
}

PooledContainer tracks a container managed by the warm pool.

Jump to

Keyboard shortcuts

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