Documentation
¶
Overview ¶
Package container provides a runtime-agnostic container integration layer for Gopherstack. It supports Docker and Podman (any OCI-compatible runtime) via a single env var switch: CONTAINER_RUNTIME=docker|podman|auto.
The Runtime interface abstracts image management and container lifecycle operations (create/start/stop/remove), volume mounts, and a simple warm-container pool with configurable idle timeout.
Index ¶
- Variables
- type APIClient
- type Config
- type DockerRuntime
- func (r *DockerRuntime) AcquireWarm(ctx context.Context, spec Spec) (*PooledContainer, error)
- func (r *DockerRuntime) Close() error
- func (r *DockerRuntime) CreateAndStart(ctx context.Context, spec Spec) (string, error)
- func (r *DockerRuntime) HasImage(ctx context.Context, imageRef string) (bool, error)
- func (r *DockerRuntime) Ping(ctx context.Context) error
- func (r *DockerRuntime) PullImage(ctx context.Context, imageRef string) error
- func (r *DockerRuntime) ReapIdleContainers(ctx context.Context)
- func (r *DockerRuntime) ReleaseContainer(containerID string) error
- func (r *DockerRuntime) StartReaper(ctx context.Context, interval time.Duration)
- func (r *DockerRuntime) StopAndRemove(ctx context.Context, containerID string) error
- type PooledContainer
- type Runtime
- type RuntimeName
- type Spec
Constants ¶
This section is empty.
Variables ¶
var ErrContainerNotFound = errors.New("container not found")
ErrContainerNotFound is returned when a requested container does not exist in the pool.
var ErrPoolExhausted = errors.New("container pool exhausted")
ErrPoolExhausted is returned when no warm container is available and the pool is full.
ErrUnavailable is returned when the container runtime is not available on the host.
var ErrUnknownRuntime = errors.New("unknown container runtime")
ErrUnknownRuntime is returned when an unrecognised runtime name is provided.
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 *dockercontainer.Config,
hostConfig *dockercontainer.HostConfig,
networkingConfig any,
platform any,
containerName string,
) (dockercontainer.CreateResponse, error)
ContainerStart(ctx context.Context, containerID string, options dockercontainer.StartOptions) error
ContainerStop(ctx context.Context, containerID string, options dockercontainer.StopOptions) error
ContainerRemove(ctx context.Context, containerID string, options dockercontainer.RemoveOptions) error
Ping(ctx context.Context) (any, error)
Close() error
}
APIClient is a subset of the Docker/Podman SDK client interface used by DockerRuntime. It is defined as an interface to enable testing without a real container daemon.
type Config ¶
type Config struct {
Logger *slog.Logger
Runtime RuntimeName
PoolSize int
IdleTimeout time.Duration
}
Config holds configuration for the container runtime layer.
type DockerRuntime ¶
type DockerRuntime struct {
// contains filtered or unexported fields
}
DockerRuntime implements Runtime using the Docker daemon.
func NewDockerRuntimeWithAPI ¶
func NewDockerRuntimeWithAPI(api APIClient, cfg Config) *DockerRuntime
NewDockerRuntimeWithAPI creates a DockerRuntime with an injected APIClient. This is primarily intended for testing; production code should use NewRuntime.
func (*DockerRuntime) AcquireWarm ¶
func (r *DockerRuntime) AcquireWarm(ctx context.Context, spec Spec) (*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: PoolSize 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 (*DockerRuntime) Close ¶
func (r *DockerRuntime) Close() error
Close releases resources held by the Docker client.
func (*DockerRuntime) CreateAndStart ¶
CreateAndStart creates a new container from spec and starts it. Returns the container ID.
func (*DockerRuntime) HasImage ¶
HasImage reports whether the given image reference is already present locally.
func (*DockerRuntime) Ping ¶
func (r *DockerRuntime) Ping(ctx context.Context) error
Ping checks whether the Docker daemon is reachable.
func (*DockerRuntime) PullImage ¶
func (r *DockerRuntime) PullImage(ctx context.Context, imageRef string) error
PullImage pulls the specified image from the registry.
func (*DockerRuntime) ReapIdleContainers ¶
func (r *DockerRuntime) ReapIdleContainers(ctx context.Context)
ReapIdleContainers stops and removes containers that have been idle longer than IdleTimeout.
func (*DockerRuntime) ReleaseContainer ¶
func (r *DockerRuntime) ReleaseContainer(containerID string) error
ReleaseContainer marks a pooled container as idle. If the container is not in the pool, ErrContainerNotFound is returned.
func (*DockerRuntime) StartReaper ¶
func (r *DockerRuntime) StartReaper(ctx context.Context, interval time.Duration)
StartReaper launches a background goroutine that periodically reaps idle containers. It stops when ctx is cancelled.
func (*DockerRuntime) StopAndRemove ¶
func (r *DockerRuntime) StopAndRemove(ctx context.Context, containerID string) error
StopAndRemove stops and removes 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 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.
type Runtime ¶
type Runtime interface {
// Ping checks whether the container daemon is reachable.
Ping(ctx context.Context) error
// PullImage pulls the specified image from the registry.
PullImage(ctx context.Context, imageRef string) error
// HasImage reports whether the given image reference is already present locally.
HasImage(ctx context.Context, imageRef string) (bool, error)
// CreateAndStart creates a new container from spec and starts it.
// Returns the container ID.
CreateAndStart(ctx context.Context, spec Spec) (string, error)
// StopAndRemove stops and removes a container.
StopAndRemove(ctx context.Context, containerID string) error
// AcquireWarm returns a warm container from the pool for the given image.
AcquireWarm(ctx context.Context, spec Spec) (*PooledContainer, error)
// ReleaseContainer marks a pooled container as idle.
ReleaseContainer(containerID string) error
// ReapIdleContainers stops and removes containers idle longer than IdleTimeout.
ReapIdleContainers(ctx context.Context)
// StartReaper launches a background goroutine that periodically reaps idle containers.
StartReaper(ctx context.Context, interval time.Duration)
// Close releases resources held by the runtime client.
Close() error
}
Runtime is the container-runtime abstraction used throughout Gopherstack. Both DockerRuntime and PodmanRuntime implement this interface.
func NewRuntime ¶
NewRuntime creates a Runtime using the configured (or auto-detected) container runtime.
The runtime is selected in the following order:
- cfg.Runtime (if non-empty)
- CONTAINER_RUNTIME environment variable
- Defaults to RuntimeDocker
RuntimeAuto probes the Docker socket first, then the Podman socket. Returns ErrUnavailable if the selected runtime is not reachable.
type RuntimeName ¶
type RuntimeName string
RuntimeName identifies which container runtime to use.
const ( // RuntimeDocker selects the Docker daemon. RuntimeDocker RuntimeName = "docker" // RuntimePodman selects the Podman daemon via its Docker-compatible socket. RuntimePodman RuntimeName = "podman" // RuntimeAuto auto-detects the available runtime by probing sockets. RuntimeAuto RuntimeName = "auto" )
type Spec ¶
type Spec struct {
// Image is the container 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
// Entrypoint overrides the image's default ENTRYPOINT.
Entrypoint []string
}
Spec holds the specification for creating a container.