This library provides a helper struct around the Docker GO SDK.
import "github.com/ATenderholt/dockerlib"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)- func SetLogger(newLogger *zap.Logger)
- type Container
- type ContainerError
- type DockerController
- func NewDockerController() (*DockerController, error)
- func (controller *DockerController) CleanupNetworks(ctx context.Context) error
- func (controller *DockerController) EnsureImage(ctx context.Context, image string) error
- func (controller *DockerController) EnsureNetwork(ctx context.Context, name string) error
- func (controller *DockerController) Remove(ctx context.Context, c Container) error
- func (controller *DockerController) Shutdown(ctx context.Context, c Container) error
- func (controller *DockerController) ShutdownAll(ctx context.Context) error
- func (controller *DockerController) Start(ctx context.Context, c Container, ready string) (chan bool, error)
- type DockerError
- type EnsureImageProgress
- type EnsureImageProgressDetail
- type NetworkError
func SetLogger(newLogger *zap.Logger)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 (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 (c Container) String() stringReturns a simplified string representation
type ContainerError struct {
// contains filtered or unexported fields
}func (e ContainerError) Error() stringDockerController 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() (*DockerController, error)NewDockerController is a helper method to create a new instance of a DockerController.
func (controller *DockerController) CleanupNetworks(ctx context.Context) errorfunc (controller *DockerController) EnsureImage(ctx context.Context, image string) errorEnsureImage is a helper method to pull the specified image to the local machine running Docker.
func (controller *DockerController) EnsureNetwork(ctx context.Context, name string) errorEnsureNetwork Creates a bridge network for the given name if it doesn't already exist.
func (controller *DockerController) Remove(ctx context.Context, c Container) errorRemove removes the specified (stopped) container based on its ID.
func (controller *DockerController) Shutdown(ctx context.Context, c Container) errorShutdown terminates the specified running Container based on its ID.
func (controller *DockerController) ShutdownAll(ctx context.Context) errorShutdownAll terminates and removes all running containers
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 struct {
// contains filtered or unexported fields
}func (e DockerError) Error() stringEnsureImageProgress is an object to unmarshall JSON returned from Docker during a pull.
type EnsureImageProgress struct {
Status string
ProgressDetail EnsureImageProgressDetail
Progress string
ID string
}func (p EnsureImageProgress) String() stringEnsureImageProgressDetail is an object to help unmarshall JSON returned from Docker during a pull.
type EnsureImageProgressDetail struct {
Current int
Total int
}type NetworkError struct {
// contains filtered or unexported fields
}func (e NetworkError) Error() stringGenerated by gomarkdoc