container

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDockerfile

func BuildDockerfile(ctx context.Context, dockerfilePath string) (string, string, error)

BuildDockerfile builds the Dockerfile at dockerfilePath. The build context is the Dockerfile's directory. It returns the image id Docker reported, the combined Docker build logs, and any error.

func CleanupAndKillContainer

func CleanupAndKillContainer(ctx context.Context, client *docker.Client, name string) error

CleanupAndKillContainer kills and removes a container by name, then removes anonymous volumes that were attached to that container.

func CreateNetwork

func CreateNetwork(ctx context.Context, name string, labels map[string]string) (bool, error)

CreateNetwork creates a Docker network if it does not already exist.

func DeleteImage

func DeleteImage(ctx context.Context, client *docker.Client, image string, tag string) error

DeleteImage removes a specific image/tag from the local machine.

func DockerAvailable

func DockerAvailable() bool

DockerAvailable returns true when a Docker daemon is reachable from the current environment.

func ImageExists

func ImageExists(ctx context.Context, client *docker.Client, image string, tag string) (bool, error)

ImageExists will return true if the image/tag exists, false otherwise. A blank tag is assumed to be "latest".

func LogsFromContainers

func LogsFromContainers(ctx context.Context, containers ...*Container) (logs.LogStreams, error)

LogsFromContainers returns one named log stream for each container. Named containers use their Docker name. Unnamed containers receive a stable container-N key based on argument order.

func MergeContainerLogs

func MergeContainerLogs(ctx context.Context, containers ...*Container) (io.ReadCloser, error)

MergeContainerLogs opens logs for N containers and merges them into one reader.

func NewClient

func NewClient(ctx context.Context) (*docker.Client, error)

func RemoveNetwork

func RemoveNetwork(ctx context.Context, name string) error

RemoveNetwork removes a Docker network. Missing networks are treated as already cleaned up.

Types

type Container

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

Container is a small wrapper around the Docker API. It owns the Docker container id, assigned host ports, environment variables, bind mounts, and anonymous volumes that should be removed during cleanup.

func NewContainer

func NewContainer(name string, image string, options ...ContainerOption) (*Container, error)

NewContainer builds a container harness for an image. A blank tag is assumed to be "latest". If a port mapping has an empty host port, scaffold will assign a free host port when the container starts.

func (*Container) Address

func (c *Container) Address(port string) string

Address returns localhost:port for a container port. If the port is not published, it returns an empty string.

func (*Container) Cleanup

func (c *Container) Cleanup(ctx context.Context) error

Cleanup kills the container if it is still running, removes it, and then removes anonymous volumes that were attached to it.

func (*Container) Client

func (c *Container) Client() *docker.Client

Client returns the Docker client used by this container.

func (*Container) DeleteImage

func (c *Container) DeleteImage(ctx context.Context) error

DeleteImage removes the container image/tag from the local machine.

func (*Container) GetContainerID

func (c *Container) GetContainerID() string

GetContainerID returns the Docker container id assigned during Start.

func (*Container) GetPorts

func (c *Container) GetPorts() map[string]string

GetPorts returns the container port to host port mapping. Host ports are available after Start has assigned them.

func (*Container) HostPort

func (c *Container) HostPort(port string) (string, bool)

HostPort returns the assigned host port for a container port.

func (*Container) ImageExists

func (c *Container) ImageExists(ctx context.Context) (bool, error)

ImageExists will return true if the image/tag exists, false otherwise. A blank tag is assumed to be "latest".

func (*Container) IsRunning

func (c *Container) IsRunning(ctx context.Context) (bool, error)

IsRunning returns true if the container is running, false otherwise.

func (*Container) Kill

func (c *Container) Kill(ctx context.Context) error

Kill immediately terminates the container without waiting for graceful shutdown inside the container.

func (*Container) Labels

func (c *Container) Labels() map[string]string

Labels returns a copy of the Docker labels configured for the container.

func (*Container) Logs

func (c *Container) Logs(ctx context.Context) (io.ReadCloser, error)

Logs returns a Docker log reader for the container.

func (*Container) Name

func (c *Container) Name() string

Name returns the configured Docker container name.

func (*Container) SetLabels

func (c *Container) SetLabels(labels map[string]string)

SetLabels merges Docker labels onto the container.

func (*Container) SetNamePrefix

func (c *Container) SetNamePrefix(prefix string)

SetNamePrefix prefixes the Docker container name if one was configured. Stacks call this before creation for services that opt into generated resource names.

func (*Container) SetNetwork

func (c *Container) SetNetwork(name string)

SetNetwork updates the Docker network name for the container. Stacks use this to attach services to a shared network before creation.

func (*Container) Start

func (c *Container) Start(ctx context.Context) (err error)

Start pulls the image if needed and starts the container. If port mappings are configured with an empty host port, a free port is assigned before the container is created.

func (*Container) Stop

func (c *Container) Stop(ctx context.Context, wait int) error

Stop sends SIGTERM and waits up to the given number of seconds. If the container does not stop in time, or wait is <= 0, it is killed with SIGKILL.

func (*Container) URL

func (c *Container) URL(scheme string, port string) string

URL returns a local URL for the requested container port.

func (*Container) WaitForLogText

func (c *Container) WaitForLogText(ctx context.Context, text string, timeout time.Duration) error

WaitForLogText waits for text to appear in the container logs.

type ContainerOption

type ContainerOption func(*Container)

ContainerOption allows callers to tune a container without making the common constructor path noisy.

func WithBind

func WithBind(hostPath string, containerPath string) ContainerOption

WithBind mounts a host path into the container. This is intentionally simple and maps directly to Docker's bind syntax.

func WithCommand

func WithCommand(command ...string) ContainerOption

WithCommand sets the container command. This is useful for images like MinIO that require a command to start the desired service.

func WithEnv

func WithEnv(env map[string]string) ContainerOption

WithEnv adds container process environment variables. These values are sent to Docker as KEY=VALUE entries when the container starts.

func WithHostIP

func WithHostIP(hostIP string) ContainerOption

WithHostIP sets the host IP used for published container ports. The default is 127.0.0.1 for local-only services.

func WithLabels

func WithLabels(labels map[string]string) ContainerOption

WithLabels adds Docker labels to the container. These labels are merged with any labels inherited from a parent stack.

func WithNetwork

func WithNetwork(name string) ContainerOption

WithNetwork attaches the container to an existing Docker network when it is created.

func WithPort

func WithPort(containerPort string, hostPort string) ContainerOption

WithPort adds one container port to host port mapping. An empty host port asks scaffold to assign a free host port when the container starts.

func WithPorts

func WithPorts(ports map[string]string) ContainerOption

WithPorts adds container port to host port mappings. An empty host port asks scaffold to assign a free host port when the container starts.

func WithTag

func WithTag(tag string) ContainerOption

WithTag sets the image tag used when the container starts. A blank tag keeps the default tag, "latest".

type Harness

type Harness interface {
	Start() error
	Stop(wait int) error
	Cleanup() error
	IsRunning() (bool, error)
}

Jump to

Keyboard shortcuts

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