docker - 尝试在 golang 中使用 exec.Command() 执行 docker pull 时,我没有看到进度条
问题描述
我正在使用 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
解决方案
正如大卫提到的,您宁愿使用官方的 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 pull hello-world时,您将获得与 docker cli 提供的相同输出
推荐阅读
- graphql - 如何使用 GraphQL 执行 Gremlin 查询?
- docker - 列表的“docker stack deploy”配置合并行为是什么?
- visual-studio - 在目标 Visual Studio 线程中打开文件
- input - 获取 DT 表上闪亮的嵌入式输入的值?
- stm32cubeide - 在 stm32l152re nucleo 中执行复位时,STM32cubeide 生成的代码首地址被擦除
- django - 如何根据 django 中的 ID 显示产品的详细信息
- python - 将 Google Cloud Run 实例连接到 MongoDB
- php - PHP:mysqli_stmt->close() 影响 stmt 的早期副本
- android - 水平滚动视图中的平滑滚动问题
- javascript - 如何在同一个表单上同时运行 2 个评分功能?