docker-client

通过docker-client 调docker API, 根据需求进行调用

 

package main

import (
    "context"
    "fmt"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
    "github.com/docker/go-connections/nat"
    "github.com/docker/docker/api/types/container"
)


func main() {
    cli, err := client.NewEnvClient()
    if err != nil {
        panic(err)
    }



    body,err:=cli.ContainerCreate(context.TODO(),&container.Config{
        Tty: true,
        OpenStdin:true,
        Image:"nginx:latest",
    },&container.HostConfig{
        PortBindings:nat.PortMap{nat.Port("80/tcp"):[]nat.PortBinding{{"0.0.0.0","10001"}}},
    },nil,nil,"testnginx2")

    if err!=nil{
        fmt.Println(err)
    }
    fmt.Println(body)
    containerID:=body.ID

    err=cli.ContainerStart(context.TODO() ,containerID, types.ContainerStartOptions{})
    fmt.Println(err)



    //containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
    //if err != nil {
    //    panic(err)
    //}
    //
    //
    //for _, container := range containers {
    //    fmt.Printf("%s %s\n", container.ID[:10], container.Image)
    //}
}

 

posted on 2021-03-04 15:43  裤裆内隐藏杀气  阅读(245)  评论(0编辑  收藏  举报

导航