dockerclient

package module
v0.0.0-...-5337229 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: MIT Imports: 15 Imported by: 0

README

dockerclient

GoDoc

Getting Started

go install github.com/james226/dockerclient

Examples

Here is a basic example showing how to spin up a container using this package:

package main

import (
	"context"
	"github.com/james226/dockerclient"
	"github.com/james226/dockerclient/options"
	"os"
	"os/signal"
	"sync"
	"syscall"
)

func main() {
	wg := &sync.WaitGroup{}
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	c, err := dockerclient.NewClient()
	if err != nil {
		panic(err)
	}

	network, err := c.Networks.Create(ctx, "example-network")
	if err != nil {
		panic(err)
	}

	image, err := c.Images.Build(ctx, "example-container", "./path/to/Dockerfile")
	if err != nil {
		panic(err)
	}

	wg.Add(1)

	opt := options.WithName("example-container").
		WithPortBinding(10000, 10000, "tcp").
		WithEnvironmentVariables(map[string]string{
			"PORT": "10000",
			"FOO":  "BAR",
		})
	container, err := c.Containers.Start(ctx, image, network, opt)
	if err != nil {
		panic(err)
	}

	go func() {
		abort := make(chan os.Signal, 1)
		signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
		<-abort

		defer wg.Done()

		container.Stop(ctx, true)
		cancel()
	}()

	wg.Wait()
}

You can find more examples of how to use this package in the examples directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	ID   string
	Name string
	// contains filtered or unexported fields
}

func (*Container) Stop

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

type ContainerOperations

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

func (ContainerOperations) Start

func (c ContainerOperations) Start(ctx context.Context, image *Image, net *Network, opts ...*options.StartContainerOptions) (*Container, error)

type DockerClient

type DockerClient struct {
	Networks   NetworkOperations
	Images     ImageOperations
	Containers ContainerOperations
	// contains filtered or unexported fields
}

func NewClient

func NewClient() (*DockerClient, error)

func (*DockerClient) Close

func (c *DockerClient) Close() error

type Image

type Image struct {
	Name string
}

type ImageOperations

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

func (ImageOperations) Build

func (i ImageOperations) Build(ctx context.Context, name string, path string, opts ...*options.BuildImageOptions) (*Image, error)

func (ImageOperations) Pull

func (i ImageOperations) Pull(ctx context.Context, name string) (*Image, error)

type Network

type Network struct {
	ID string
}

type NetworkOperations

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

func (NetworkOperations) Create

func (n NetworkOperations) Create(ctx context.Context, name string) (*Network, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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