golang操作docker - sevck
2021-07-20 12:12:00 Author: www.cnblogs.com(查看原文) 阅读量:126 收藏

官网文档:

https://pkg.go.dev/github.com/docker/docker/client#Client.ContainerRemove

官方SDK示例:

https://docs.docker.com/engine/api/sdk/examples/

但是给的不是很全,搜了下有的SDK使用方式太老了,新版本的docker不适用。

package check

import (
    "context"
    "fmt"
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    "github.com/docker/docker/api/types/mount"
    "github.com/docker/docker/client"
    log "github.com/sirupsen/logrus"
    "os"
)

const (
    XtraBackup23  string = "xtrabackup2_3" //镜像名称
    XtraBackup24  string = "xtrabackup2_4" //镜像名称
    XtraBackup80  string = "xtrabackup8_0" //镜像名称
    containerName string = "xtrabackup-latest"    //容器名称
    indexName     string = "/" + containerName    //容器索引名称,用于检查该容器是否存在是使用
    CMD           string = "/bin/bash"            //运行的cmd命令,用于启动container中的程序
    WorkDir       string = "/tmp/"                //container工作目录
    ContainerDir  string = "/home"                 //容器挂载目录
    HostDir       string = "/tmp"                 //容器挂在到宿主机的目录
    n             int    = 5                      //每5s检查一个容器是否在运行
)

// 获取docker镜像列表
func ListImages() []string {
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        log.Error(err)
        os.Exit(0)
    }

    images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
    if err != nil {
        log.Error(err)
        os.Exit(0)
    }
    var imagesLib []string
    for _, image := range images {
        imagesLib = append(imagesLib, image.RepoTags[0])
    }
    return imagesLib
}

// 容器列表
func ListContainer() {
    cli, err := client.NewClientWithOpts(client.FromEnv)
    if err != nil {
        log.Error(err)
        os.Exit(0)
    }

    containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
    if err != nil {
        log.Error(err)
        os.Exit(0)
    }

    for _, container := range containers {
        fmt.Printf("%s\n", container.Image)
    }
}


func CreateContainer(images string) string {
    //创建容器
    //cli, err := client.NewClientWithOpts(client.FromEnv)
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        log.Error(err)
        os.Exit(0)
    }
    cont, err := cli.ContainerCreate(ctx, &container.Config{
        Image:      images,
        Cmd:        []string{CMD}, //docker 容器中执行的命令
        Tty:        true,          //docker run命令中的-t选项
        OpenStdin:  true,          //docker run命令中的-i选项
        WorkingDir: WorkDir,       //docker容器中的工作目录
        //Volumes: map[string]struct{}{"/tmp":""},
    }, &container.HostConfig{ // 挂载
        //Mounts: m,
        Mounts: []mount.Mount{
            {
                Type:   mount.TypeBind,
                //Source: HostDir,
                Source: "/Users/sevck/Desktop/",
                Target: ContainerDir,
            },
        },
    }, nil, nil, "")
    if err != nil {
        log.Error(err)
        //stopContainer()
        os.Exit(0)
    }
    log.Info(cont.ID)
    return cont.ID
}

//启动容器
func StartContainer(containerID string) {
    ctx := context.Background()
    cli, _ := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    err := cli.ContainerStart(ctx, containerID, types.ContainerStartOptions{})
    if err != nil {
        log.Error("启动错误:",err)
    }
}

//停止容器
func StopContainer(containerID string){
    ctx := context.Background()
    cli, _ := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    err := cli.ContainerStop(ctx,containerID, nil)
    if err != nil {
        log.Error(err)
    }else {
        log.Info("stop", containerID)
    }
}


func RemoveContainer(containerID string){
    ctx := context.Background()
    cli, _ := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    err := cli.ContainerRemove(ctx,containerID,types.ContainerRemoveOptions{
        //RemoveLinks: true,
        //RemoveVolumes: true,
        Force: true,
    })
    if err != nil{
        log.Error(err)
    }
}

【版权所有@Sevck 博客地址http://www.cnblogs.com/sevck】 可以转载,注明出处.


文章来源: http://www.cnblogs.com/sevck/p/15033922.html
如有侵权请联系:admin#unsafe.sh