尝试使用 exec 执行 docker 拉取时.golang 中的命令(),我没有看到进度条

  • 本文关键字:命令 exec 执行 docker golang 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 提到的,你更愿意使用官方的 docker 引擎 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 cli 在执行docker pull hello-world时提供的相同输出

最新更新