container

package
v0.1.2-0...-3e0559f Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package container implements a fluent interface for creating and starting docker containers.

Package container implements a fluent interface for creating and starting docker containers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder interface {
	WithClient(client client.APIClient) Builder
	WithImage(imageWithTag string) Builder
	WithEnv(env []string) Builder
	WithPortBindings(portList []PortBinding) Builder
	WithPortRanges(portRanges []PortRangeBinding) Builder
	WithMountPoints(mounts []mount.Mount) Builder
	WithName(name string) Builder
	WithNetworkAliases(aliases ...string) Builder
	WithNetworkMode(networkMode string) Builder
	WithNetworks(networks []string) Builder
	WithLabels(labels map[string]string) Builder
	WithLogConfig(config *container.LogConfig) Builder
	WithRegistryAuth(auth *imageHelper.RegistryAuth) Builder
	WithAutoRemove(remove bool) Builder
	WithRestartPolicy(policy RestartPolicyName) Builder
	WithEntrypoint(cmd []string) Builder
	WithCmd(cmd []string) Builder
	WithShell(shell []string) Builder
	WithUser(uid *int64) Builder
	WithLogWriter(logger io.StringWriter) Builder
	WithoutConflict() Builder
	WithForcePullImage() Builder
	WithExtraHosts(hosts []string) Builder
	WithPreCreateHooks(hooks ...LifecycleFunc) Builder
	WithPostCreateHooks(hooks ...LifecycleFunc) Builder
	WithPreStartHooks(hooks ...LifecycleFunc) Builder
	WithPostStartHooks(hooks ...LifecycleFunc) Builder
	Create() (Container, error)
	CreateAndStart() (Container, error)
	CreateAndStartWaitUntilExit() (Container, *WaitResult, error)
}

A Builder handles the process of creating and starting containers, it can be configured using 'With...' methods. A Builder can be created using the NewDockerBuilder method.

func NewDockerBuilder

func NewDockerBuilder(ctx context.Context) Builder

A shorthand function for creating a new DockerContainerBuilder and calling WithClient. Creates a default Docker client which can be overwritten using 'WithClient'. Creates a default logger which logs using the 'fmt' package.

type Container

type Container interface {
	GetName() string
	GetContainerID() *string
	GetNetworkIDs() []string
	Start(context.Context, client.APIClient) error
	StartWaitUntilExit(context.Context, client.APIClient) (*WaitResult, error)
}

Container is the abstract result of the builder

type DockerContainer

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

func NewDockerContainer

func NewDockerContainer(cont *types.Container, preStartHooks, postStartHooks *[]LifecycleFunc, mountList *[]mount.Mount) DockerContainer

func (DockerContainer) GetContainerID

func (d DockerContainer) GetContainerID() *string

func (DockerContainer) GetName

func (d DockerContainer) GetName() string

func (DockerContainer) GetNetworkIDs

func (d DockerContainer) GetNetworkIDs() []string

func (DockerContainer) Start

func (DockerContainer) StartWaitUntilExit

func (d DockerContainer) StartWaitUntilExit(ctx context.Context, cli client.APIClient) (*WaitResult, error)

type DockerContainerBuilder

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

func (*DockerContainerBuilder) Create

func (dc *DockerContainerBuilder) Create() (Container, error)

Creates the container using the configuration given by 'With...' functions.

func (*DockerContainerBuilder) CreateAndStart

func (dc *DockerContainerBuilder) CreateAndStart() (Container, error)

func (*DockerContainerBuilder) CreateAndStartWaitUntilExit

func (dc *DockerContainerBuilder) CreateAndStartWaitUntilExit() (Container, *WaitResult, error)

func (*DockerContainerBuilder) WithAutoRemove

func (dc *DockerContainerBuilder) WithAutoRemove(remove bool) Builder

Sets if the container should be removed after it exists.

func (*DockerContainerBuilder) WithClient

func (dc *DockerContainerBuilder) WithClient(cli client.APIClient) Builder

Sets the Docker client of the ContainerBuilder. By default NewDockerBuilder creates a client.

func (*DockerContainerBuilder) WithCmd

func (dc *DockerContainerBuilder) WithCmd(cmd []string) Builder

Sets the CMD of a container.

func (*DockerContainerBuilder) WithEntrypoint

func (dc *DockerContainerBuilder) WithEntrypoint(entrypoint []string) Builder

Sets the entrypoint of a container.

func (*DockerContainerBuilder) WithEnv

func (dc *DockerContainerBuilder) WithEnv(envList []string) Builder

Sets the environment variables of a container. Values are in a "KEY=VALUE" format.

func (*DockerContainerBuilder) WithExtraHosts

func (dc *DockerContainerBuilder) WithExtraHosts(hosts []string) Builder

Sets the builder to use extra hosts when creating the container. Hosts must be defined in a "HOSTNAME:IP" format.

func (*DockerContainerBuilder) WithForcePullImage

func (dc *DockerContainerBuilder) WithForcePullImage() Builder

Sets the builder to force pull the image before creating the container.

func (*DockerContainerBuilder) WithImage

func (dc *DockerContainerBuilder) WithImage(imageWithTag string) Builder

Sets the image of a container in a "image:tag" format where image can be a fully qualified name.

func (*DockerContainerBuilder) WithLabels

func (dc *DockerContainerBuilder) WithLabels(labels map[string]string) Builder

Sets the labels of a container.

func (*DockerContainerBuilder) WithLogConfig

func (dc *DockerContainerBuilder) WithLogConfig(logConfig *container.LogConfig) Builder

Sets the log config of the container.

func (*DockerContainerBuilder) WithLogWriter

func (dc *DockerContainerBuilder) WithLogWriter(logger io.StringWriter) Builder

Sets the logger which logs messages releated to the builder (and not the container).

func (*DockerContainerBuilder) WithMountPoints

func (dc *DockerContainerBuilder) WithMountPoints(mountList []mount.Mount) Builder

Sets mount points of a container.

func (*DockerContainerBuilder) WithName

func (dc *DockerContainerBuilder) WithName(name string) Builder

Sets the name of the target container.

func (*DockerContainerBuilder) WithNetworkAliases

func (dc *DockerContainerBuilder) WithNetworkAliases(aliases ...string) Builder

Sets the network aliases used when connecting the container to the network. Applied only if the NetworkMode is not none/host. It is a must for Podman, without this the DNS resolution won't work.

func (*DockerContainerBuilder) WithNetworkMode

func (dc *DockerContainerBuilder) WithNetworkMode(networkMode string) Builder

Sets the network mode of a container.

func (*DockerContainerBuilder) WithNetworks

func (dc *DockerContainerBuilder) WithNetworks(networks []string) Builder

Sets the extra networks.

func (*DockerContainerBuilder) WithPortBindings

func (dc *DockerContainerBuilder) WithPortBindings(portList []PortBinding) Builder

Sets the port bindings of a container. Expose internal container ports to the host.

func (*DockerContainerBuilder) WithPortRanges

func (dc *DockerContainerBuilder) WithPortRanges(portRanges []PortRangeBinding) Builder

Sets port ranges of a container.

func (*DockerContainerBuilder) WithPostCreateHooks

func (dc *DockerContainerBuilder) WithPostCreateHooks(hooks ...LifecycleFunc) Builder

Sets an array of hooks which runs after the container is created.

func (*DockerContainerBuilder) WithPostStartHooks

func (dc *DockerContainerBuilder) WithPostStartHooks(hooks ...LifecycleFunc) Builder

Sets an array of hooks which runs after the container is started.

func (*DockerContainerBuilder) WithPreCreateHooks

func (dc *DockerContainerBuilder) WithPreCreateHooks(hooks ...LifecycleFunc) Builder

Sets an array of hooks which runs before the container is created. ContainerID is nil in these hooks.

func (*DockerContainerBuilder) WithPreStartHooks

func (dc *DockerContainerBuilder) WithPreStartHooks(hooks ...LifecycleFunc) Builder

Sets an array of hooks which runs before the container is started.

func (*DockerContainerBuilder) WithRegistryAuth

func (dc *DockerContainerBuilder) WithRegistryAuth(auth *imageHelper.RegistryAuth) Builder

Sets the registry and authentication for the given image.

func (*DockerContainerBuilder) WithRestartPolicy

func (dc *DockerContainerBuilder) WithRestartPolicy(policy RestartPolicyName) Builder

Sets the restart policy of the container.

func (*DockerContainerBuilder) WithShell

func (dc *DockerContainerBuilder) WithShell(shell []string) Builder

Sets the SHELL of a container.

func (*DockerContainerBuilder) WithTTY

func (dc *DockerContainerBuilder) WithTTY(tty bool) Builder

Sets if standard streams should be attached to a tty.

func (*DockerContainerBuilder) WithUser

func (dc *DockerContainerBuilder) WithUser(user *int64) Builder

Sets the UID.

func (*DockerContainerBuilder) WithoutConflict

func (dc *DockerContainerBuilder) WithoutConflict() Builder

Deletes the container with the given name if already exists.

type DockerExecBuilder

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

func NewExecBuilder

func NewExecBuilder(ctx context.Context, containerID *string) *DockerExecBuilder

A shorthand function for creating a new DockerExecBuilder and calling WithClient. Creates a default Docker client which can be overwritten using 'WithClient'. Creates a default logger which logs using the 'fmt' package.

func (*DockerExecBuilder) Create

func (de *DockerExecBuilder) Create() (Exec, error)

Creates the exec command using the configuration given by 'With...' functions.

func (*DockerExecBuilder) WithAttachStderr

func (de *DockerExecBuilder) WithAttachStderr() *DockerExecBuilder

Sets the builder to attach stderr to the exec command

func (*DockerExecBuilder) WithAttachStdin

func (de *DockerExecBuilder) WithAttachStdin() *DockerExecBuilder

Sets the builder to attach stdin to the exec command

func (*DockerExecBuilder) WithAttachStdout

func (de *DockerExecBuilder) WithAttachStdout() *DockerExecBuilder

Sets the builder to attach stdout to the exec command

func (*DockerExecBuilder) WithClient

func (de *DockerExecBuilder) WithClient(cli *client.Client) *DockerExecBuilder

Sets the Docker client of the ExecBuilder. By default NewExecBuilder creates a client.

func (*DockerExecBuilder) WithCmd

func (de *DockerExecBuilder) WithCmd(cmd []string) *DockerExecBuilder

Sets the commands to run as part of exec

func (*DockerExecBuilder) WithDetach

func (de *DockerExecBuilder) WithDetach() *DockerExecBuilder

Sets the builder to detach from the exec command

func (*DockerExecBuilder) WithLogWriter

func (de *DockerExecBuilder) WithLogWriter(logger io.StringWriter) *DockerExecBuilder

Sets the logger which logs messages releated to the builder (and not the container).

func (*DockerExecBuilder) WithPrivileged

func (de *DockerExecBuilder) WithPrivileged() *DockerExecBuilder

Sets the builder to run the exec process with extended privileges.

func (*DockerExecBuilder) WithTTY

func (de *DockerExecBuilder) WithTTY() *DockerExecBuilder

Sets if standard streams should be attached to a tty.

func (*DockerExecBuilder) WithUser

func (de *DockerExecBuilder) WithUser(user *int64) *DockerExecBuilder

Sets the UID.

func (*DockerExecBuilder) WithWorkingDir

func (de *DockerExecBuilder) WithWorkingDir(workingDir string) *DockerExecBuilder

Sets the working directory for the exec process inside the container.

type Exec

type Exec struct {
	ExecID string
	// contains filtered or unexported fields
}

func (Exec) Start

func (e Exec) Start() error

type ExecBuilder

type ExecBuilder interface {
	WithAttachStderr() *DockerExecBuilder
	WithAttachStdin() *DockerExecBuilder
	WithAttachStdout() *DockerExecBuilder
	WithClient(client *client.Client) *DockerExecBuilder
	WithCmd(cmd []string) *DockerExecBuilder
	WithDetach() *DockerExecBuilder
	WithLogWriter(logger io.StringWriter) *DockerExecBuilder
	WithPrivileged() *DockerExecBuilder
	WithTTY() *DockerExecBuilder
	WithUser(user *int64) *DockerExecBuilder
	WithWorkingDir(workingDir string) *DockerExecBuilder
	Create() (Exec, error)
}

An ExecBuilder handles the process of creating and starting exec, it can be configured using 'With...' methods. An ExecBuilder can be created using the DockerExecBuilder method.

type LifecycleFunc

type LifecycleFunc func(ctx context.Context, client client.APIClient, containerName string,
	containerId *string, mountList []mount.Mount, logger *io.StringWriter) error

Hook function which can be used to add custom logic before and after events of the lifecycle of a container. 'containerId' can be nil depending on the hook.

type PortBinding

type PortBinding struct {
	PortBinding *uint16 `json:"portBinding" binding:"gte=0,lte=65535"`
	ExposedPort uint16  `json:"exposedPort" binding:"required,gte=0,lte=65535"`
}

The PortBinding struct defines port bindings of a container. ExposedPort is the port in the container, while PortBinding is the port on the host.

func (PortBinding) String

func (p PortBinding) String() string

same style as default String() method, need this because one is optional

type PortRange

type PortRange struct {
	From uint16 `json:"from" binding:"required,gte=0,lte=65535"`
	To   uint16 `json:"to" binding:"required,gtefield=From,lte=65535"`
}

PortRange defines a range of ports from 0 to 65535.

type PortRangeBinding

type PortRangeBinding struct {
	Internal PortRange `json:"internal" binding:"required"`
	External PortRange `json:"external" binding:"required"`
}

PortRangeBinding defines port range bindings of a container. Internal is the port in the container, while External is the port on the host.

type RestartPolicyName

type RestartPolicyName string

RestartPolicyName defines the restart policy used by a container.

const (
	EmptyRestartPolicy                RestartPolicyName = ""
	AlwaysRestartPolicy               RestartPolicyName = "always"
	RestartUnlessStoppedRestartPolicy RestartPolicyName = "unless-stopped"
	NoRestartPolicy                   RestartPolicyName = "no"
	OnFailureRestartPolicy            RestartPolicyName = "on-failure"
)

func (RestartPolicyName) MarshalJSON

func (policy RestartPolicyName) MarshalJSON() ([]byte, error)

custom enum marshal JSON interface implementation

func (*RestartPolicyName) UnmarshalJSON

func (policy *RestartPolicyName) UnmarshalJSON(b []byte) error

custom enum unmarshal JSON interface implementation

type RestartPolicyUnmarshalInvalidError

type RestartPolicyUnmarshalInvalidError struct{}

RestartPolicyUnmarshalInvalidError represents custom error regarding restart policy

func (*RestartPolicyUnmarshalInvalidError) Error

type WaitResult

type WaitResult struct {
	StatusCode int64
	Logs       []string
}

WaitResult with the status code from the container

Jump to

Keyboard shortcuts

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