• Go SDK 操作Docker


    下载包

    go get "github.com/docker/docker/api/types"
    go get "github.com/docker/docker/client"
    

    创建Container

    其主要复杂在端口的映射上面。如果hostConfig里面的PortBindings对应的PortMap中相应端口对应的PortBinding,其值有几个(空值也算)就会对外映射几个端口。

    func createContainer(cli *client.Client) string {
        exports := make(nat.PortSet, 10)
        port, err := nat.NewPort("tcp", "80")
        log(err)
        exports[port] = struct{}{}
        config := &container.Config{Image: "nginx", ExposedPorts: exports}
    
        portBind := nat.PortBinding{HostPort: "80"}
        portMap := make(nat.PortMap, 0)
        tmp := make([]nat.PortBinding, 0, 1)
        tmp = append(tmp, portBind)
        portMap[port] = tmp
        hostConfig := &container.HostConfig{PortBindings: portMap}
        // networkingConfig := &network.NetworkingConfig{}
        containerName := "hel"
        body, err := cli.ContainerCreate(context.Background(), config, hostConfig, nil, containerName)
        log(err)
        fmt.Printf("ID: %s
    ", body.ID)
        return body.ID
    }
    

    启动/停止/删除

    package main
    
    import (
        "context"
        "fmt"
        "time"
    
        "github.com/docker/docker/api/types"
        "github.com/docker/docker/api/types/container"
        "github.com/docker/docker/client"
        "github.com/docker/go-connections/nat"
    )
    
    func main() {
        cli, err := client.NewClient("tcp://192.168.100.33:2376", "v1.12", nil, nil)
        defer cli.Close()
        log(err)
        listImage(cli)
        id := createContainer(cli)
        fmt.Printf("%s
    ", id)
        time.Sleep(time.Second * 1)
        startContainer(id, cli)
        time.Sleep(time.Second * 40)
        stopContainer(id, cli)
        time.Sleep(time.Second * 3)
        id, err = removeContainer(id, cli)
        if err == nil {
            fmt.Println("删除容器", id, "成功")
        }
    }
    // 列出镜像
    func listImage(cli *client.Client) {
        images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
        log(err)
    
        for _, image := range images {
            fmt.Println(image)
        }
    }
    // 创建容器
    func createContainer(cli *client.Client) string {
        exports := make(nat.PortSet, 10)
        port, err := nat.NewPort("tcp", "80")
        log(err)
        exports[port] = struct{}{}
        config := &container.Config{Image: "nginx", ExposedPorts: exports}
    
        portBind := nat.PortBinding{HostPort: "80"}
        portMap := make(nat.PortMap, 0)
        tmp := make([]nat.PortBinding, 0, 1)
        tmp = append(tmp, portBind)
        portMap[port] = tmp
        hostConfig := &container.HostConfig{PortBindings: portMap}
        
        containerName := "hel"
        body, err := cli.ContainerCreate(context.Background(), config, hostConfig, nil, containerName)
        log(err)
        fmt.Printf("ID: %s
    ", body.ID)
        return body.ID
    }
    // 启动
    func startContainer(containerID string, cli *client.Client) {
        err := cli.ContainerStart(context.Background(), containerID, types.ContainerStartOptions{})
        log(err)
        if err == nil {
            fmt.Println("容器", containerID, "启动成功")
        }
    }
    // 停止
    func stopContainer(containerID string, cli *client.Client) {
        timeout := time.Second * 10
        err := cli.ContainerStop(context.Background(), containerID, &timeout)
        if err != nil {
            log(err)
        } else {
            fmt.Printf("容器%s已经被停止
    ", containerID)
        }
    }
    // 删除
    func removeContainer(containerID string, cli *client.Client) (string, error) {
        err := cli.ContainerRemove(context.Background(), containerID, types.ContainerRemoveOptions{})
        log(err)
        return containerID, err
    }
    
    func log(err error) {
        if err != nil {
            fmt.Printf("%v
    ", err)
            panic(err)
        }
    }
    

    端口映射

    在使用docker go sdk的时候,进行容器创建,如果指定了映射的端口,会报以下错误:

    cannot use ports (type map["bitbucket.org/myorg/myproject/vendor/github.com/docker/go-connect
    ions/nat".Port]struct {}) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortSet in field
     value
    

    其解决方法:把gopath里面pkg下面docker的vendor里面相应的connections包删除即可。src下的最好也删除。如果编译报缺少包,则把其相应的包get下来即可。

    注意: 客户端版本和docker版本兼容问题

  • 相关阅读:
    常用排序算法--合并排序和快速排序
    常用排序算法--冒泡排序及改进和插入排序时间复杂度分析
    常用数据结构图--拓扑排序
    常用数据结构栈的应用—-表达式求值
    Session原理,生命周期
    jsp内置对象out 和response.getwriter().write()的区别
    div中的div在父容器中水平居中或者垂直居中
    <!DOCTYPE html> 到底是什么意思?
    设置了环境变量,为什么执行javac报找不到javac: 找不到文件
    自动抽取邮件内容
  • 原文地址:https://www.cnblogs.com/you-men/p/14539684.html
Copyright © 2020-2023  润新知