v0.1.0-rc2 Latest Latest

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

Go to latest
Published: Jan 28, 2015 License: Apache-2.0, Apache-2.0 Imports: 16 Imported by: 0


Docker client library in Go


Well maintained docker client library.


package main

import (

// Callback used to listen to Docker's events
func eventCallback(event *dockerclient.Event, ec chan error, args ...interface{}) {
	log.Printf("Received event: %#v\n", *event)

func main() {
	// Init the client
	docker, _ := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)

	// Get only running containers
	containers, err := docker.ListContainers(false)
	if err != nil {
	for _, c := range containers {
		log.Println(c.Id, c.Names)

	// Inspect the first container returned
	if len(containers) > 0 {
		id := containers[0].Id
		info, _ := docker.InspectContainer(id)

	// Create a container
	containerConfig := &dockerclient.ContainerConfig{Image: "ubuntu:12.04", Cmd: []string{"bash"}}
	containerId, err := docker.CreateContainer(containerConfig)
	if err != nil {

	// Start the container
	err = docker.StartContainer(containerId)
	if err != nil {

	// Stop the container (with 5 seconds timeout)
	docker.StopContainer(containerId, 5)

	// Listen to events
	docker.StartMonitorEvents(eventCallback, nil)
	time.Sleep(3600 * time.Second)




View Source
const (
	APIVersion = "v1.15"


View Source
var (
	ErrNotFound = errors.New("Not found")


This section is empty.


type AuthConfig

type AuthConfig struct {
	Username string `json:"username,omitempty"`
	Password string `json:"password,omitempty"`
	Email    string `json:"email,omitempty"`

AuthConfig hold parameters for authenticating with the docker registry

type Callback

type Callback func(*Event, chan error, ...interface{})

type Client

type Client interface {
	Info() (*Info, error)
	ListContainers(all, size bool, filters string) ([]Container, error)
	InspectContainer(id string) (*ContainerInfo, error)
	CreateContainer(config *ContainerConfig, name string) (string, error)
	ContainerLogs(id string, options *LogOptions) (io.ReadCloser, error)
	Exec(config *ExecConfig) (string, error)
	StartContainer(id string, config *HostConfig) error
	StopContainer(id string, timeout int) error
	RestartContainer(id string, timeout int) error
	KillContainer(id, signal string) error
	StartMonitorEvents(cb Callback, ec chan error, args ...interface{})
	Version() (*Version, error)
	PullImage(name string, auth *AuthConfig) error
	RemoveContainer(id string, force bool) error
	ListImages() ([]*Image, error)
	RemoveImage(name string) error
	PauseContainer(name string) error
	UnpauseContainer(name string) error

type Container

type Container struct {
	Id         string
	Names      []string
	Image      string
	Command    string
	Created    int64
	Status     string
	Ports      []Port
	SizeRw     int64
	SizeRootFs int64

type ContainerConfig

type ContainerConfig struct {
	Hostname        string
	Domainname      string
	User            string
	Memory          int64
	MemorySwap      int64
	CpuShares       int64
	Cpuset          string
	AttachStdin     bool
	AttachStdout    bool
	AttachStderr    bool
	PortSpecs       []string
	ExposedPorts    map[string]struct{}
	Tty             bool
	OpenStdin       bool
	StdinOnce       bool
	Env             []string
	Cmd             []string
	Image           string
	Volumes         map[string]struct{}
	WorkingDir      string
	Entrypoint      []string
	NetworkDisabled bool
	OnBuild         []string

	// This is used only by the create command
	HostConfig HostConfig

type ContainerInfo

type ContainerInfo struct {
	Id      string
	Created string
	Path    string
	Name    string
	Args    []string
	ExecIDs []string
	Config  *ContainerConfig
	State   struct {
		Running    bool
		Paused     bool
		Restarting bool
		Pid        int
		ExitCode   int
		StartedAt  time.Time
		FinishedAt time.Time
		Ghost      bool
	Image           string
	NetworkSettings struct {
		IpAddress   string
		IpPrefixLen int
		Gateway     string
		Bridge      string
		Ports       map[string][]PortBinding
	SysInitPath    string
	ResolvConfPath string
	Volumes        map[string]string
	HostConfig     *HostConfig

type DockerClient

type DockerClient struct {
	URL        *url.URL
	HTTPClient *http.Client
	TLSConfig  *tls.Config
	// contains filtered or unexported fields

func NewDockerClient

func NewDockerClient(daemonUrl string, tlsConfig *tls.Config) (*DockerClient, error)

func NewDockerClientTimeout

func NewDockerClientTimeout(daemonUrl string, tlsConfig *tls.Config, timeout time.Duration) (*DockerClient, error)

func (*DockerClient) ContainerLogs

func (client *DockerClient) ContainerLogs(id string, options *LogOptions) (io.ReadCloser, error)

func (*DockerClient) CreateContainer

func (client *DockerClient) CreateContainer(config *ContainerConfig, name string) (string, error)

func (*DockerClient) Exec

func (client *DockerClient) Exec(config *ExecConfig) (string, error)

func (*DockerClient) Info

func (client *DockerClient) Info() (*Info, error)

func (*DockerClient) InspectContainer

func (client *DockerClient) InspectContainer(id string) (*ContainerInfo, error)

func (*DockerClient) KillContainer

func (client *DockerClient) KillContainer(id, signal string) error

func (*DockerClient) ListContainers

func (client *DockerClient) ListContainers(all bool, size bool, filters string) ([]Container, error)

func (*DockerClient) ListImages

func (client *DockerClient) ListImages() ([]*Image, error)

func (*DockerClient) PauseContainer

func (client *DockerClient) PauseContainer(id string) error

func (*DockerClient) PullImage

func (client *DockerClient) PullImage(name string, auth *AuthConfig) error

func (*DockerClient) RemoveContainer

func (client *DockerClient) RemoveContainer(id string, force bool) error

func (*DockerClient) RemoveImage

func (client *DockerClient) RemoveImage(name string) error

func (*DockerClient) RestartContainer

func (client *DockerClient) RestartContainer(id string, timeout int) error

func (*DockerClient) StartContainer

func (client *DockerClient) StartContainer(id string, config *HostConfig) error

func (*DockerClient) StartMonitorEvents

func (client *DockerClient) StartMonitorEvents(cb Callback, ec chan error, args ...interface{})

func (*DockerClient) StopAllMonitorEvents

func (client *DockerClient) StopAllMonitorEvents()

func (*DockerClient) StopContainer

func (client *DockerClient) StopContainer(id string, timeout int) error

func (*DockerClient) UnpauseContainer

func (client *DockerClient) UnpauseContainer(id string) error

func (*DockerClient) Version

func (client *DockerClient) Version() (*Version, error)

type Error

type Error struct {
	StatusCode int
	Status     string
	// contains filtered or unexported fields

func (Error) Error

func (e Error) Error() string

type Event

type Event struct {
	Id     string
	Status string
	From   string
	Time   int64

type ExecConfig

type ExecConfig struct {
	AttachStdin  bool
	AttachStdout bool
	AttachStderr bool
	Tty          bool
	Cmd          []string
	Container    string
	Detach       bool

type HostConfig

type HostConfig struct {
	Binds           []string
	ContainerIDFile string
	LxcConf         []map[string]string
	Privileged      bool
	PortBindings    map[string][]PortBinding
	Links           []string
	PublishAllPorts bool
	Dns             []string
	DnsSearch       []string
	VolumesFrom     []string
	NetworkMode     string
	RestartPolicy   RestartPolicy

type Image

type Image struct {
	Created     int64
	Id          string
	ParentId    string
	RepoTags    []string
	Size        int64
	VirtualSize int64

type Info

type Info struct {
	ID              string
	Containers      int64
	Driver          string
	DriverStatus    [][]string
	ExecutionDriver string
	Images          int64
	KernelVersion   string
	OperatingSystem string
	NCPU            int64
	MemTotal        int64
	Name            string
	Labels          []string

type LogOptions

type LogOptions struct {
	Follow     bool
	Stdout     bool
	Stderr     bool
	Timestamps bool
	Tail       int64

type Port

type Port struct {
	IP          string
	PrivatePort int
	PublicPort  int
	Type        string

type PortBinding

type PortBinding struct {
	HostIp   string
	HostPort string

type RespContainersCreate

type RespContainersCreate struct {
	Id       string
	Warnings []string

type RestartPolicy

type RestartPolicy struct {
	Name              string
	MaximumRetryCount int64

type Version

type Version struct {
	Version   string
	GitCommit string
	GoVersion string


Path Synopsis

Jump to

Keyboard shortcuts

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