如何在构建后通过API获取Docker镜像的ID?

huangapple go评论92阅读模式
英文:

How to obtain Docker image ID from API after building?

问题

根据Docker v1.18 API文档/build方法接受一个包含Dockerfile的TAR流,并尝试构建镜像。

然而,除非我漏掉了什么,似乎没有办法在构建过程完成后获取镜像的ID。

例如,我正在使用go-dockerclient库连接到Docker API,并使用以下代码片段构建镜像:

//...

opts := docker.BuildImageOptions{
    Name:         "test-image",
    InputStream:  input,
    OutputStream: output,
}

if err := client.BuildImage(opts); err != nil {
    fmt.Println(err)
}

//...

BuildImage()方法执行后,我可以通过Docker的CLI客户端确认镜像确实已创建。然而,BuildImage()方法只返回一个错误值,似乎没有办法获取镜像的ID。

我是否漏掉了什么?

英文:

According to the Docker v1.18 API documentation, the /build method accepts a TAR stream (with a Dockerfile) and attempts to build the image.

However, unless I'm missing something, there seems to be no way to obtain the image ID after the build process completes.

For example, I'm using the go-dockerclient library to connect to the Docker API and build the image using the following snippet:

//...

opts := docker.BuildImageOptions{
	Name:         "test-image",
	InputStream:  input,
	OutputStream: output,
}

if err := client.BuildImage(opts); err != nil {
	fmt.Println(err)
}

//...

The BuildImage() method executes and I can confirm (through Docker's CLI client) that the image is indeed created. However, the only value returned from BuildImage() is an error. There doesn't seem to be a way to obtain the ID of the image.

Am I missing something?

答案1

得分: 1

使用InspectImage函数。它将返回一个带有ID字段的Image:https://godoc.org/github.com/fsouza/go-dockerclient#Client.InspectImage

英文:

Use the InspectImage function. It will give you an Image with an ID field: https://godoc.org/github.com/fsouza/go-dockerclient#Client.InspectImage

huangapple
  • 本文由 发表于 2015年5月23日 05:49:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/30406363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定