Docker APIをGoから使う #dockerjp

17 January 2015

Ian Lewis

無職

お前誰よ

Docker Remote API

http://docs.docker.com/reference/api/docker_remote_api/

Kubernetes

http://kubernetes.io/

Go Docker Client

インストール

$ 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)
    //...
}

おれおれ docker run

    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)

おれおれ docker ps

    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)
    }

おれおれ docker stop/rm

        client.StopContainer(c.ID, 30)
        client.RemoveContainer(docker.RemoveContainerOptions{
            ID: c.ID,
            RemoveVolumes: true,
        })

Thank you