dockermanage

package
v3.27.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package dockermanage provides lightweight Docker container lifecycle helpers for integration testing.

A Manager wraps the native Docker client and exposes methods to start, stop, remove, and list containers. Containers are configured through functional Option values such as WithImage, WithContainerPortTCP, and WithEnv.

After starting a container, use Manager.WaitReady with a custom ReadinessFunc to block until the service inside the container is accepting connections.

Every container created through this package is tagged with the ManagedLabelKey label, which allows bulk operations like Manager.StopManaged and Manager.RemoveManaged to clean up all managed containers.

Database-specific sub-packages (e.g., postgres) provide opinionated defaults for common databases.

Index

Constants

View Source
const (
	// DefaultHostIP is the default host IP used for port bindings.
	DefaultHostIP = "127.0.0.1"

	// ManagedLabelKey marks containers created by this package. The value indicates the container
	// type (e.g., "postgres"). Presence of the key means the container is managed.
	ManagedLabelKey = "pressly.goose"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	ID     string
	Image  string
	Host   string
	Port   int
	Labels map[string]string
}

Container is a running Docker container managed by this package.

type ExecOptions

type ExecOptions struct {
	Cmd    []string  // Command and arguments
	Env    []string  // Optional environment variables
	Stdout io.Writer // Where to write stdout (nil → discard)
	Stderr io.Writer // Where to write stderr (nil → discard)
}

ExecOptions configures a command to run inside a container.

type ExecResult

type ExecResult struct {
	ExitCode int
}

ExecResult holds the outcome of an exec invocation.

type Manager

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

Manager manages Docker containers using the native Docker client.

func NewManager

func NewManager(logger *slog.Logger) (*Manager, error)

NewManager creates a new manager backed by the Docker client configured from environment.

func (*Manager) Close

func (m *Manager) Close() error

Close closes the underlying Docker client.

func (*Manager) Exec

func (m *Manager) Exec(ctx context.Context, containerID string, opts ExecOptions) (*ExecResult, error)

Exec runs a command inside a running container and streams its output.

func (*Manager) ListManaged

func (m *Manager) ListManaged(ctx context.Context) ([]string, error)

ListManaged returns all container IDs started by this package.

func (*Manager) Remove

func (m *Manager) Remove(ctx context.Context, containerID string) error

Remove removes a container. If running, it is force removed.

func (*Manager) RemoveManaged

func (m *Manager) RemoveManaged(ctx context.Context) error

RemoveManaged removes all containers started by this package.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, options ...Option) (_ *Container, retErr error)

Start starts a container with the provided options.

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context, containerID string) error

Stop stops a running container.

func (*Manager) StopManaged

func (m *Manager) StopManaged(ctx context.Context) error

StopManaged stops all containers started by this package.

func (*Manager) WaitReady

func (m *Manager) WaitReady(ctx context.Context, container *Container, readiness ReadinessFunc, opts ...WaitOption) error

WaitReady waits until a custom readiness checker succeeds.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures container start behavior.

func WithAutoRemove

func WithAutoRemove(autoRemove bool) Option

WithAutoRemove configures Docker AutoRemove behavior.

func WithContainerPort

func WithContainerPort(port string) Option

WithContainerPort sets the container port to expose, for example: "5432/tcp".

func WithContainerPortTCP

func WithContainerPortTCP(port int) Option

WithContainerPortTCP is a convenience helper for TCP ports.

func WithEnv

func WithEnv(key, value string) Option

WithEnv appends a single environment variable.

func WithEnvVars

func WithEnvVars(envVars []string) Option

WithEnvVars appends environment variables in KEY=VALUE format.

func WithHostIP

func WithHostIP(hostIP string) Option

WithHostIP sets the host IP to bind the container port to.

func WithHostPort

func WithHostPort(port int) Option

WithHostPort sets a fixed host port. Leave unset to auto-assign.

func WithImage

func WithImage(image string) Option

WithImage sets the container image (for example: postgres:16-alpine).

func WithLabel

func WithLabel(key, value string) Option

WithLabel sets a single container label.

func WithLabels

func WithLabels(labels map[string]string) Option

WithLabels merges labels into container labels.

func WithName

func WithName(name string) Option

WithName sets the container name.

func WithPullProgress

func WithPullProgress(w io.Writer) Option

WithPullProgress sets where image pull output is streamed.

type ReadinessFunc

type ReadinessFunc func(ctx context.Context, container *Container) error

ReadinessFunc reports whether a container is ready.

type WaitOption

type WaitOption func(*waitConfig)

WaitOption configures WaitReady behavior.

func WithDelay

func WithDelay(d time.Duration) WaitOption

WithDelay sets the interval between readiness checks. Defaults to 500ms.

func WithTimeout

func WithTimeout(d time.Duration) WaitOption

WithTimeout sets the maximum time to wait for readiness. Defaults to 30s.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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