Giter VIP home page Giter VIP logo

dockerclient's Introduction

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.

dockerclient's People

Contributors

vieux avatar aluzzardi avatar samalba avatar ehazlett avatar donhcd avatar crosbymichael avatar jimmyxian avatar discordianfish avatar abronan avatar devfacet avatar tonistiigi avatar epipho avatar mountkin avatar bradrydzewski avatar philips avatar dnephin avatar divideandconquer avatar mssola avatar daaku avatar tianon avatar pwnall avatar dennisdenuto avatar 06wagon avatar wrotki avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.