基于go实现的在容器中执行命令的封装库

package main

import (
	"bytes"
	"context"
	"fmt"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/client"
	"github.com/docker/docker/pkg/stdcopy"
	"io"
	"log"
	"time"
)

/*
 library for executing commands in containers based on go
*/

type DockerClient struct {
	timeout int // docker client request timeout
	client  *client.Client
}

// ExecResult exec result
type ExecResult struct {
	StdOut   string // std output
	StdErr   string // std error
	ExitCode int    // exit code
}

func NewDockerClient(timeout int) (*DockerClient, error) {
	if timeout < 0 {
		return nil, fmt.Errorf("negative numbers are not allowed")
	}
	if timeout == 0 {
		timeout = 10
	}
	return &DockerClient{timeout: timeout}, nil
}

// Open open docker cli
func (dc *DockerClient) Open() error {
	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(), client.WithTimeout(time.Duration(dc.timeout)*time.Second))
	if err != nil {
		return err
	}
	dc.client = cli
	return nil
}

// Close close docker cli
func (dc *DockerClient) Close() error {
	if dc.client == nil {
		return nil
	}
	return dc.client.Close()
}

// Exec exec command
func (dc *DockerClient) Exec(ctx context.Context, containerID string, command []string) (types.IDResponse, error) {
	if dc.client == nil {
		return types.IDResponse{}, fmt.Errorf("docker cli has been closed")
	}
	config := types.ExecConfig{
		AttachStdout: true,
		AttachStderr: true,
		Cmd:          command,
	}
	return dc.client.ContainerExecCreate(ctx, containerID, config)
}

// InspectExecResp get exec command
func (dc *DockerClient) InspectExecResp(ctx context.Context, id string) (ExecResult, error) {
	if dc.client == nil {
		return ExecResult{}, fmt.Errorf("docker cli has been closed")
	}
	var execResult ExecResult
	resp, err := dc.client.ContainerExecAttach(ctx, id, types.ExecStartCheck{})
	if err != nil {
		return execResult, err
	}
	defer resp.Close()
	// read the output
	var outBuf, errBuf bytes.Buffer
	outputDone := make(chan error)

	go func() {
		// StdCopy demultiplexes the stream into two buffers
		_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
		outputDone <- err
	}()

	select {
	case err := <-outputDone:
		if err != nil {
			return execResult, err
		}
		break

	case <-ctx.Done():
		return execResult, ctx.Err()
	}
	stdout, err := io.ReadAll(&outBuf)
	if err != nil {
		return execResult, err
	}
	stderr, err := io.ReadAll(&errBuf)
	if err != nil {
		return execResult, err
	}
	res, err := dc.client.ContainerExecInspect(ctx, id)
	if err != nil {
		return execResult, err
	}
	execResult.ExitCode = res.ExitCode
	execResult.StdOut = string(stdout)
	execResult.StdErr = string(stderr)
	return execResult, nil
}

func main() {
	containerID := "redis" // containerName or containerID
	command1 := []string{"ls", "-l"}
	command2 := []string{"uname", "-a"}
	dockerClient, err := NewDockerClient(3)
	if err != nil {
		log.Fatal(err)
	}
	err = dockerClient.Open()
	if err != nil {
		log.Fatal(err)
	}
	defer func(dockerClient *DockerClient) {
		err := dockerClient.Close()
		if err != nil {
			log.Fatal(err)
		}
	}(dockerClient)
	idResponse1, err := dockerClient.Exec(context.Background(), containerID, command1)
	if err != nil {
		log.Fatal(err)
	}
	resp1, err := dockerClient.InspectExecResp(context.Background(), idResponse1.ID)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v\n", resp1)
	idResponse2, err := dockerClient.Exec(context.Background(), containerID, command2)
	if err != nil {
		log.Fatal(err)
	}
	resp2, err := dockerClient.InspectExecResp(context.Background(), idResponse2.ID)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v", resp2)
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赴前尘

喜欢我的文章?请我喝杯咖啡吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值