dockerlib

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2022 License: BSD-3-Clause Imports: 15 Imported by: 0

README

dockerlib

This library provides a helper struct around the Docker GO SDK.

import "github.com/ATenderholt/dockerlib"

Example

package example

ctx = context.TODO()
controller, err := dockerlib.NewDockerController()

container := dockerlib.Container{
    Name:  "example",
    Image: "alpine",
    Mounts: []mount.Mount{
        {
            Source:      "/abs/path/to/src",
            Target:      "target",
            Type:        mount.TypeBind,
            ReadOnly:    ...,
            Consistency: ...,
        },
    },
    Ports: map[int]int{
        from: to,
	},
    Command:     []string{...},
    Environment: []string{"KEY=VALUE"},
    Network:     []string{"example"},
}

// make sure network 'example' exists
err := controller.EnsureNetwork(ctx, "example")

// start the container, return channel receives value when given substring is
// found
ready, err := controller.Start(ctx, container, "Container is ready")

<-ready

controller.ShutdownAll(ctx)
controller.CleanupNetworks(ctx)

Documentation

func SetLogger

func SetLogger(newLogger *zap.Logger)

type Container

Container represents a simplified interface for starting a Docker container

type Container struct {
    Name        string
    Image       string
    ID          string
    Mounts      []mount.Mount
    Ports       map[int]int
    Command     []string
    Environment []string
    Network     []string
}
func (Container) PortBindings
func (c Container) PortBindings() (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error)

PortBindings Helper method to return the structs required to start a Docker container, or any error

func (Container) String
func (c Container) String() string

Returns a simplified string representation

type ContainerError

type ContainerError struct {
    // contains filtered or unexported fields
}
func (ContainerError) Error
func (e ContainerError) Error() string

type DockerController

DockerController is a concrete type that can be used to control Docker containers using its SDK.

type DockerController struct {
    // contains filtered or unexported fields
}
func NewDockerController
func NewDockerController() (*DockerController, error)

NewDockerController is a helper method to create a new instance of a DockerController.

func (*DockerController) CleanupNetworks
func (controller *DockerController) CleanupNetworks(ctx context.Context) error
func (*DockerController) EnsureImage
func (controller *DockerController) EnsureImage(ctx context.Context, image string) error

EnsureImage is a helper method to pull the specified image to the local machine running Docker.

func (*DockerController) EnsureNetwork
func (controller *DockerController) EnsureNetwork(ctx context.Context, name string) error

EnsureNetwork Creates a bridge network for the given name if it doesn't already exist.

func (*DockerController) Remove
func (controller *DockerController) Remove(ctx context.Context, c Container) error

Remove removes the specified (stopped) container based on its ID.

func (*DockerController) Shutdown
func (controller *DockerController) Shutdown(ctx context.Context, c Container) error

Shutdown terminates the specified running Container based on its ID.

func (*DockerController) ShutdownAll
func (controller *DockerController) ShutdownAll(ctx context.Context) error

ShutdownAll terminates and removes all running containers

func (*DockerController) Start
func (controller *DockerController) Start(ctx context.Context, c Container, ready string) (chan bool, error)

Start is the method used to Start a Docker container using the specified Container c. It also automatically follows logs and creates a channel that is used to indicate when a running container is ready according to the provided ready string.

type DockerError

type DockerError struct {
    // contains filtered or unexported fields
}
func (DockerError) Error
func (e DockerError) Error() string

type EnsureImageProgress

EnsureImageProgress is an object to unmarshall JSON returned from Docker during a pull.

type EnsureImageProgress struct {
    Status         string
    ProgressDetail EnsureImageProgressDetail
    Progress       string
    ID             string
}
func (EnsureImageProgress) String
func (p EnsureImageProgress) String() string

type EnsureImageProgressDetail

EnsureImageProgressDetail is an object to help unmarshall JSON returned from Docker during a pull.

type EnsureImageProgressDetail struct {
    Current int
    Total   int
}

type NetworkError

type NetworkError struct {
    // contains filtered or unexported fields
}
func (NetworkError) Error
func (e NetworkError) Error() string

Generated by gomarkdoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadLinesAsBytes added in v1.2.1

func ReadLinesAsBytes(reader io.Reader) <-chan []byte

func SetLogger

func SetLogger(newLogger *zap.Logger)

Types

type Container

type Container struct {
	Name        string
	Image       string
	ID          string
	Mounts      []mount.Mount
	Ports       map[int]int
	Command     []string
	Environment []string
	Network     []string
}

Container represents a simplified interface for starting a Docker container

func (Container) PortBindings

func (c Container) PortBindings() (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error)

PortBindings Helper method to return the structs required to start a Docker container, or any error

func (Container) String

func (c Container) String() string

Returns a simplified string representation

type ContainerError

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

func (ContainerError) Error

func (e ContainerError) Error() string

type DockerController

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

DockerController is a concrete type that can be used to control Docker containers using its SDK.

func NewDockerController

func NewDockerController() (*DockerController, error)

NewDockerController is a helper method to create a new instance of a DockerController.

func (*DockerController) CleanupNetworks added in v1.0.0

func (controller *DockerController) CleanupNetworks(ctx context.Context) error

func (*DockerController) EnsureImage added in v0.9.1

func (controller *DockerController) EnsureImage(ctx context.Context, image string) error

EnsureImage is a helper method to pull the specified image to the local machine running Docker.

func (*DockerController) EnsureNetwork added in v1.0.0

func (controller *DockerController) EnsureNetwork(ctx context.Context, name string) error

EnsureNetwork Creates a bridge network for the given name if it doesn't already exist.

func (DockerController) GetContainerHostPath added in v1.3.0

func (controller DockerController) GetContainerHostPath(ctx context.Context, name string, path string) (string, error)

func (*DockerController) Remove added in v1.1.0

func (controller *DockerController) Remove(ctx context.Context, c Container) error

Remove removes the specified (stopped) container based on its ID.

func (*DockerController) Shutdown

func (controller *DockerController) Shutdown(ctx context.Context, c Container) error

Shutdown terminates the specified running Container based on its ID.

func (*DockerController) ShutdownAll

func (controller *DockerController) ShutdownAll(ctx context.Context) error

ShutdownAll terminates and removes all running containers

func (*DockerController) Start

func (controller *DockerController) Start(ctx context.Context, c *Container, ready string) (chan bool, error)

Start is the method used to Start a Docker container using the specified Container c. It also automatically follows logs and creates a channel that is used to indicate when a running container is ready according to the provided ready string.

func (*DockerController) WaitForShutdown added in v1.2.0

func (controller *DockerController) WaitForShutdown(ctx context.Context, c Container, timeout time.Duration) error

WaitForShutdown blocks until the specified Container has shutdown or errors within given timeout.

type DockerError

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

func (DockerError) Error

func (e DockerError) Error() string

type EnsureImageProgress

type EnsureImageProgress struct {
	Status         string
	ProgressDetail EnsureImageProgressDetail
	Progress       string
	ID             string
}

EnsureImageProgress is an object to unmarshall JSON returned from Docker during a pull.

func (EnsureImageProgress) String

func (p EnsureImageProgress) String() string

type EnsureImageProgressDetail

type EnsureImageProgressDetail struct {
	Current int
	Total   int
}

EnsureImageProgressDetail is an object to help unmarshall JSON returned from Docker during a pull.

type NetworkError added in v1.0.0

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

func (NetworkError) Error added in v1.0.0

func (e NetworkError) Error() string

Jump to

Keyboard shortcuts

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