docker

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 10 Imported by: 17

README

Docker sdk wrapper

This package provides a wrapper for the docker sdk, creating and running commands in containers.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCmd an infinite command to keep the container running.
	DefaultCmd = Command{"sh", "-c", "while true; do sleep 1; done"}
)

Functions

This section is empty.

Types

type Command

type Command []string

func (Command) String

func (c Command) String() string

type Container

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

func CreateContainer

func CreateContainer(ctx context.Context, cfg ContainerConfig) (*Container, error)

CreateContainer returns an instance of a created Container, use this if you want to perform things before starting the container. Eg attaching IO.

func NewContainer

func NewContainer(ctx context.Context, cfg ContainerConfig) (*Container, error)

func StartContainer

func StartContainer(ctx context.Context, cfg ContainerConfig) (*Container, error)

StartContainer returns an instance of a started Container, use this for running multiple Exec inside the same Container.

func (*Container) AttachIO

func (c *Container) AttachIO(stdout io.Writer, stdin io.Reader) error

AttachIO attaches stdout/stdin to the Command running in the container

func (*Container) Create

func (c *Container) Create() error

Create the docker container with the defined config.

func (*Container) Exec

func (c *Container) Exec(cmd Command, cfg RunConfig) (*Exec, error)

Exec a command inside the container.

func (*Container) Inspect

func (c *Container) Inspect(ctx context.Context) (types.ContainerJSON, error)

Inspect the container.

func (*Container) Remove

func (c *Container) Remove() error

Remove the container, use this to delete an exited container.

func (*Container) Start

func (c *Container) Start(timeout time.Duration) error

Start the container and wait for it to be running or finished.

func (*Container) Stop

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

Stop kills the container if it's running the DefaultCmd otherwise it gracefully shuts it down.

func (*Container) Wait

func (c *Container) Wait() error

Wait for the container to stop running, helpful when container starts with a non DefaultCmd.

type ContainerConfig

type ContainerConfig struct {
	Image       string // Required
	Logger      *zap.Logger
	Labels      map[string]string // container labels name:value
	Directories map[string]string // Directories to mount to container src:target
	WorkingDir  string            // Required
	Name        string            // ContainerName defaults to randomly generated
	// The Command to start the container with, defaults to DefaultCmd
	Cmd Command

	// Automatically remove the container when it exits
	AutoRemove bool
	// contains filtered or unexported fields
}

ContainerConfig contains the configuration data about a container.

func (*ContainerConfig) String

func (cc *ContainerConfig) String() string

String is the command as it can be run from the terminal.

type ContainerStatus

type ContainerStatus string
const (
	ContainerStatusRunning ContainerStatus = "running"
)

type Exec

type Exec struct {
	types.IDResponse
	// contains filtered or unexported fields
}

Exec is a standalone command running in an already running container.

func (*Exec) AttachIO

func (e *Exec) AttachIO(stdout io.Writer, stdin io.Reader) error

AttachIO binds the I/O of the Exec.

func (*Exec) Start

func (e *Exec) Start() error

Start starts the Exec, if the Exec is attached it starts automatically on attachment.

func (*Exec) Wait

func (e *Exec) Wait() error

Wait for the Exec to finish.

type RunConfig

type RunConfig struct {
	Stdin  io.Reader
	Stdout io.Writer
	Daemon bool
}

RunConfig is the config you can run multiple Exec in a container and capturing its output.

Jump to

Keyboard shortcuts

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