1

任何知道如何使用 docker 的 go 客户端 engine-api 观看 docker 事件的人,或者有任何例子吗?

4

1 回答 1

3

我知道现在该怎么做:

package main

import (
    "encoding/json"
    "fmt"
    "io"
    "log"

    "github.com/docker/engine-api/client"
    "github.com/docker/engine-api/types"
    "github.com/docker/engine-api/types/events"
    "golang.org/x/net/context"
)

func main() {

    defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
    cli, err := client.NewClient("http://172.17.150.101:2376", "v1.24", nil, defaultHeaders)
    if err != nil {
        panic(err)
    }

    options := types.ContainerListOptions{All: true}
    containers, err := cli.ContainerList(context.Background(), options)
    if err != nil {
        panic(err)
    }

    for _, c := range containers {
        fmt.Println(c.ID)
    }

    body, err := cli.Events(context.Background(), types.EventsOptions{})
    if err != nil {
        log.Fatal(err)
    }

    dec := json.NewDecoder(body)
    for {
        var event events.Message
        err := dec.Decode(&event)
        if err != nil && err == io.EOF {
            break
        }

        log.Println(event)
    }
}
于 2016-08-04T06:05:55.807 回答