1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-23 17:22:09 +01:00
drone-cli/vendor/github.com/samalba/dockerclient
2015-10-24 17:06:46 -07:00
..
examples cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
mockclient cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
auth_test.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
auth.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
dockerclient_test.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
dockerclient.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
engine_mock_test.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
example_responses.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
interface.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
LICENSE cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
README.md cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
types.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00
utils.go cli now working with 0.4 woohoo 2015-10-24 17:06:46 -07:00

Docker client library in Go

GoDoc

Well maintained docker client library.

How to use it?

Here is an example showing how to use it:

package main

import (
	"github.com/samalba/dockerclient"
	"log"
	"time"
	"os"
)

// 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, false, "")
	if err != nil {
		log.Fatal(err)
	}
	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)
		log.Println(info)
	}

	// Build a docker image
	// some.tar contains the build context (Dockerfile any any files it needs to add/copy)
	dockerBuildContext, err := os.Open("some.tar")
	defer dockerBuildContext.Close()
	buildImageConfig := &dockerclient.BuildImage{
			Context:        dockerBuildContext,
			RepoName:       "your_image_name",
			SuppressOutput: false,
	}
	reader, err := docker.BuildImage(buildImageConfig)
	if err != nil {
		log.Fatal(err)
	}

	// Create a container
	containerConfig := &dockerclient.ContainerConfig{
		Image: "ubuntu:14.04",
		Cmd:   []string{"bash"},
		AttachStdin: true,
		Tty:   true}
	containerId, err := docker.CreateContainer(containerConfig, "foobar")
	if err != nil {
		log.Fatal(err)
	}

	// Start the container
	hostConfig := &dockerclient.HostConfig{}
	err = docker.StartContainer(containerId, hostConfig)
	if err != nil {
		log.Fatal(err)
	}

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

	// Listen to events
	docker.StartMonitorEvents(eventCallback, nil)

	// Hold the execution to look at the events coming
	time.Sleep(3600 * time.Second)
}

Maintainers

List of people you can ping for feedback on Pull Requests or any questions.