docker

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RunIDEnv lets CI pin the run ID (e.g. to Jenkins' BUILD_TAG) so separate
	// gtool invocations of one build share it and other builds stay isolated.
	RunIDEnv = "GTOOL_RUN_ID"
	// RunLabel is the container label that scopes resources to one run.
	RunLabel = "gtool-run"
)

Variables

This section is empty.

Functions

func ExitCode added in v1.0.0

func ExitCode(inspect *types.ContainerJSON, err error) (int, error)

ExitCode extracts the exit code of a finished container from the result of InspectContainer. A failed inspect or a missing state is an error: assuming 0 would report a run nobody observed as passed.

func HostUser added in v1.0.0

func HostUser() string

func ResolveRunID added in v1.0.0

func ResolveRunID(generate bool) string

ResolveRunID returns the run ID from GTOOL_RUN_ID. When unset it returns a random ID if generate is true (single-invocation pipelines) or "" otherwise, which keeps the legacy, unscoped behaviour.

Types

type Client

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

func NewClient

func NewClient(logger *zap.Logger) (*Client, error)

func NewRunClient added in v1.0.0

func NewRunClient(logger *zap.Logger) (*Client, error)

NewRunClient returns a client scoped to one pipeline run (GTOOL_RUN_ID or a generated ID). Every single-invocation pipeline (test, stress) must use it so concurrent runs never see or tear down each other's containers.

func (*Client) Close

func (c *Client) Close() error

func (*Client) CopyFileToContainer added in v1.0.0

func (c *Client) CopyFileToContainer(ctx context.Context, containerID, dir, name string, data []byte) error

CopyFileToContainer writes data as dir/name inside the container; dir must exist.

func (*Client) CopyToContainer

func (c *Client) CopyToContainer(ctx context.Context, containerID, targetPath string, content io.Reader) error

func (*Client) CreateContainer

func (c *Client) CreateContainer(ctx context.Context, config *ContainerConfig) (string, error)

func (*Client) EnsureImage

func (c *Client) EnsureImage(ctx context.Context, imageName string) error

EnsureImage makes an image available, preferring a local copy: if the image already exists locally it skips the pull, otherwise it pulls from the registry.

func (*Client) ExecInContainer

func (c *Client) ExecInContainer(ctx context.Context, containerID string, config *ExecConfig) (string, error)

func (*Client) FollowContainerLogs added in v1.0.0

func (c *Client) FollowContainerLogs(ctx context.Context, containerID string, tail int, w io.Writer) error

FollowContainerLogs streams the container's logs (starting with the last tail lines) to w until the container stops or ctx is cancelled; a cancellation (Ctrl-C) is a normal end, not an error.

func (*Client) GetContainerLogs

func (c *Client) GetContainerLogs(ctx context.Context, containerID string, tail int) (string, error)

func (*Client) ImageExists

func (c *Client) ImageExists(ctx context.Context, imageName string) (bool, error)

ImageExists reports whether an image is already present in the local Docker image store, so callers can avoid a registry pull for local-only images (e.g. freshly built ones).

func (*Client) InspectContainer

func (c *Client) InspectContainer(ctx context.Context, containerID string) (*types.ContainerJSON, error)

func (*Client) IsContainerRunning

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

IsContainerRunning reports whether the container runs. A container that has already exited is an ErrContainerExited error, so readiness loops fail fast instead of polling a dead container until their budget runs out.

func (*Client) ListContainersByLabels

func (c *Client) ListContainersByLabels(ctx context.Context, labels map[string]string) ([]types.Container, error)

func (*Client) Ping

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

func (*Client) PullImage

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

func (*Client) RemoveContainer

func (c *Client) RemoveContainer(ctx context.Context, containerID string, force bool) error

func (*Client) RemoveContainerByName

func (c *Client) RemoveContainerByName(ctx context.Context, name string, labels map[string]string) (bool, error)

RemoveContainerByName force-removes the container with exactly this name, in any state, when it carries every label in labels; it is a no-op when none exists. A same-named container without those labels was not created by gtool: it is left alone and ErrContainerForeign is returned.

func (*Client) RunContainer added in v1.0.0

func (c *Client) RunContainer(ctx context.Context, config *ContainerConfig) (string, error)

RunContainer creates and starts a container. When the start fails (e.g. the host port is already allocated) the created container is removed, so a failed launch never leaves an orphan behind.

func (*Client) RunID added in v1.0.0

func (c *Client) RunID() string

func (*Client) SetAllRuns added in v1.0.0

func (c *Client) SetAllRuns(all bool)

func (*Client) SetRunID added in v1.0.0

func (c *Client) SetRunID(id string)

func (*Client) StartContainer

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

func (*Client) StopContainer

func (c *Client) StopContainer(ctx context.Context, containerID string, timeout *int) error

func (*Client) WaitForContainer

func (c *Client) WaitForContainer(ctx context.Context, containerID string, condition container.WaitCondition) error

func (*Client) WaitForHealthy

func (c *Client) WaitForHealthy(ctx context.Context, containerID string, interval time.Duration, retries int) error

type ContainerConfig

type ContainerConfig struct {
	Image        string
	Name         string
	Entrypoint   []string
	Cmd          []string
	Env          []string
	PortBindings map[string]string
	Mounts       []Mount
	NetworkMode  string
	ExtraHosts   []string
	AutoRemove   bool
	Init         bool
	User         string
	Labels       map[string]string
}

type ExecConfig

type ExecConfig struct {
	Cmd          []string
	AttachStdout bool
	AttachStderr bool
	WorkingDir   string
	Env          []string
}

type Mount

type Mount struct {
	Type     string
	Source   string
	Target   string
	ReadOnly bool
}

Jump to

Keyboard shortcuts

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