Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Repository files navigation

Docker client library in Go

GoDoc

No longer well-maintained docker client library. Docker's supported engine API client for go is docker/engine-api.

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 eventsfunceventCallback(event*dockerclient.Event, ecchanerror, args...interface{}) {
log.Printf("Received event: %#v\n", *event)
}
funcmain() {
// Init the clientdocker, _:=dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
// Get only running containerscontainers, err:=docker.ListContainers(false, false, "")
iferr!=nil {
log.Fatal(err)
}
for_, c:=rangecontainers {
log.Println(c.Id, c.Names)
}
// Inspect the first container returnediflen(containers) >0 {
id:=containers[0].Idinfo, _:=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")
deferdockerBuildContext.Close()
buildImageConfig:=&dockerclient.BuildImage{
Context: dockerBuildContext,
RepoName: "your_image_name",
SuppressOutput: false,
}
reader, err:=docker.BuildImage(buildImageConfig)
iferr!=nil {
log.Fatal(err)
}
// Create a containercontainerConfig:=&dockerclient.ContainerConfig{
Image: "ubuntu:14.04",
Cmd: []string{"bash"},
AttachStdin: true,
Tty: true}
containerId, err:=docker.CreateContainer(containerConfig, "foobar", nil)
iferr!=nil {
log.Fatal(err)
}
// Start the containerhostConfig:=&dockerclient.HostConfig{}
err=docker.StartContainer(containerId, hostConfig)
iferr!=nil {
log.Fatal(err)
}
// Stop the container (with 5 seconds timeout)docker.StopContainer(containerId, 5)
// Listen to eventsdocker.StartMonitorEvents(eventCallback, nil)
// Hold the execution to look at the events comingtime.Sleep(3600*time.Second)
}

Maintainers

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

About

Docker client library in Go

Topics

Resources

Stars

317 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages