英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论