doulan3436 2019-04-17 09:46
浏览 1038
已采纳

通过Docker API在正在运行的容器中执行powershell命令

I want to execute a powershell command in a docker container running on a windows host.

The specific command I want to execute is "powershell Get-PSDrive C | Select-Object Used,Free"

I have implemented this using the Docker API for python and it is simple like calling:

cmd = "powershell Get-PSDrive C | Select-Object Used,Free"
output = container.exec_run(cmd)

This works as intended, but I need to implement this in golang.

But somehow, it is not clear for me how to interact with the Docker API for golang. I looked into the API and was confused by the hijackedSession. How do I have to setup the calls for ContainerExecCreate, ContainerExecAttach and ContainerExecStart ?

I expect the golang script to deliver the same results like the python code does:

        Used         Free
        ----         ----
199181606912 307151622144

Which then can be parsed by me.

  • 写回答

1条回答 默认 最新

  • doupang3062 2019-04-21 15:03
    关注

    The HijackedResponse struct:

    type HijackedResponse struct {
        Conn   net.Conn
        Reader *bufio.Reader
    }
    
    
    

    You need to copy the response from the resp.Reader,here is my code:

    package main
    
    import (
        "bytes"
        "context"
        "fmt"
        "github.com/docker/docker/api/types"
        "github.com/docker/docker/client"
        "github.com/docker/docker/pkg/stdcopy"
        "strings"
    )
    
    func readFromCommand() (string, error) {
        cli, err := client.NewEnvClient()
        if err != nil {
            return "", err
        }
        ctx := context.Background()
        config := types.ExecConfig{
            Cmd:          strings.Split("powershell Get-PSDrive C | Select-Object Used,Free", " "),
            AttachStdout: true,
            AttachStderr: true,
        }
        response, err := cli.ContainerExecCreate(ctx,
            // container id
            "cf59d65ab1", config)
        if err != nil {
            return "", err
        }
        execID := response.ID
    
        resp, err := cli.ContainerExecAttach(ctx, execID, config)
        if err != nil {
            return "", err
        }
        defer resp.Close()
        stdout := new(bytes.Buffer)
        stderr := new(bytes.Buffer)
        _, err = stdcopy.StdCopy(stdout, stderr, resp.Reader)
        if err != nil {
            return "", err
        }
        s := stdout.String()
        fmt.Println(s)
        i := stderr.String()
        fmt.Println(i)
        return s, nil
    
    }
    
    

    Do remember to change the container id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格