docker - 尝试在golang中使用exec.Command()执行docker pull时,我看不到进度条

标签 docker go

我正在使用Go exec程序包执行docker pull debian命令:

import (
    "bufio"
    "os/exec"
    "strings"
)

func main() {
    cmd := exec.Command("docker", "pull", "debian")
    stdout, _ := cmd.StdoutPipe()
    cmd.Start()
    scanner := bufio.NewScanner(stdout)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }
    return nil
}
但这从未显示出进度条。仅在完全完成时显示更新。对于GB上的较大图像,很难查看是否有进展。它显示了以下内容:
e9afc4f90ab0: Pulling fs layer
e9afc4f90ab0: Verifying Checksum
e9afc4f90ab0: Download complete
e9afc4f90ab0: Pull complete
是否有可能获得类似于在终端中运行docker pull debian时看到的输出或可以用来显示进度的输出?:
e9afc4f90ab0: Downloading [==========>                                        ]  10.73MB/50.39MB

最佳答案

正如David所提到的,您宁愿使用official docker engine SDK与docker进行交互。
初始化Docker客户端

cli, _ := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
拉图像
reader, _ := cli.ImagePull(context.Background(), "hello-world", types.ImagePullOptions{})
解析json流
id, isTerm := term.GetFdInfo(os.Stdout)
_ = jsonmessage.DisplayJSONMessagesStream(reader, os.Stdout, id, isTerm, nil)
执行docker pull hello-world时,将获得与docker cli提供的输出相同的输出。

关于docker - 尝试在golang中使用exec.Command()执行docker pull时,我看不到进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62587083/

相关文章:

node.js - greenlock-express 在哪里保存 Lets-Encrypt 证书?

postgresql - 如何使用单个 docker 命令删除和重新创建 postgres 数据库?

sql - 我的 SQL 代码应该放在 MVC 中的什么位置

mongodb - 从Mongo Collection游标追加嵌套结构

file - base64 解码器(io.Reader 实现)不当行为

mongodb - 如何将 mongodb 投影与 Go 和 mgo 一起使用?

docker - 的区别是什么?和./在RUN中

node.js - 在 Docker compose 中运行 gulp - 不会创建文件

docker - 如何检查 docker 镜像里面有什么?

gorilla mux http 包