gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:09:46 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com