Docker APIをGoから使う #dockerjp
17 January 2015
Ian Lewis
無職
Ian Lewis
無職
インストール
$ go get github.com/fsouza/go-dockerclient
クライアント作成
package main // 'docker' package import "github.com/fsouza/go-dockerclient" func main() { endpoint := "unix:///var/run/docker.sock" client, _ := docker.NewClient(endpoint) //... }
c, _ := client.CreateContainer(docker.CreateContainerOptions{ Name: "my-nginx", Config: &docker.Config{ Image: "nginx", }, }) client.StartContainer(c.ID, &docker.HostConfig{ PortBindings: map[docker.Port][]docker.PortBinding{ "80/tcp": []docker.PortBinding{ docker.PortBinding{HostPort: "8000"}, }, }, Binds: []string{"/opt/workspace/src/github.com/IanMLewis/docker_meetup_slides:/usr/share/nginx/html:ro"}, }) fmt.Println("ID: ", c.ID) fmt.Println("Name: ", c.Name) fmt.Println("Created: ", c.Created)
clist, _ := client.ListContainers(docker.ListContainersOptions{All: false}) for _, c := range clist { fmt.Println("ID: ", c.ID) fmt.Println("Name: ", c.Names) fmt.Println("Created: ", c.Created) }
client.StopContainer(c.ID, 30) client.RemoveContainer(docker.RemoveContainerOptions{ ID: c.ID, RemoveVolumes: true, })