doubo1883 2018-06-25 17:19
浏览 1072
已采纳

我可以固定docker API版本:客户端版本1.38太新了。 支持的最大API版本为1.37

Is there a way to pin docker API version using the golang client? (short of using dep for vendoring)

code below fails with

client version 1.38 is too new. Maximum supported API version is 1.37

This code ran fine until recently

go version go1.9.5 linux/amd64

here is : docker version

Client:
 Version:      18.05.0-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   f150324
 Built:        Wed May  9 22:16:25 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.05.0-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   f150324
  Built:        Wed May  9 22:14:32 2018
  OS/Arch:      linux/amd64
  Experimental: false

this kicks up the API version mismatch

package main

// kill off some containers

import (
    "fmt"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
    "golang.org/x/net/context"

    "strings"
)

func main() {
    ctx := context.Background()
    cli, err := client.NewEnvClient()
    if err != nil {
        panic(err) // <--- crashes here due to API mismatch
    }

    containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
    if err != nil {
        panic(err)
    }

    for _, container := range containers {

        if strings.Contains(container.Image, "enduser") || strings.Contains(container.Image, "admin") {

            fmt.Println("
 we found enduser or admin so lets stop it
")

            fmt.Print("Stopping container ", container.ID[:10], "... ")
            if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
                panic(err)
            }
            fmt.Println("Success")
        }
    }
}

In English, the above error is because the default client version of the github repo client library is newer than version supported by Docker ... so to address the comment - the solution is to request a lower version of the repo library to match Docker, not to request a higher version

  • 写回答

2条回答 默认 最新

  • dswg47377 2018-06-25 17:24
    关注

    You can ask for a version specifically with NewClientWithOpts().

    package main
    
    import (
        "net/http"
    
        "github.com/docker/docker/api/types/container"
        "github.com/docker/docker/client"
        "golang.org/x/net/context"
    )
    
    func main() {
        ctx := context.Background()
        cli, err := client.NewClientWithOpts(client.WithVersion("1.37"))
        if err != nil {
            panic(err)
        }
    }
    

    See Versioned API and SDK. At the tail end it brings up using the Go API and (tries) to link to the relevant code:

    You can specify the API version to use, in one of the following ways:

    The docs hard link to a line number on the master branch which has probably changed, but the code above should provide you with enough context to understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题