|
@@ -3,7 +3,6 @@ package util
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
"fmt"
|
|
"fmt"
|
|
- "github.com/docker/docker/api/types/image"
|
|
|
|
"github.com/docker/docker/client"
|
|
"github.com/docker/docker/client"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -17,21 +16,18 @@ func ImageExists(cli *client.Client, imageName string) bool {
|
|
// 设置上下文
|
|
// 设置上下文
|
|
ctx := context.Background()
|
|
ctx := context.Background()
|
|
|
|
|
|
- // 获取所有的镜像
|
|
|
|
- images, err := cli.ImageList(ctx, image.ListOptions{})
|
|
|
|
|
|
+ // 检查镜像是否存在
|
|
|
|
+ _, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
|
if err != nil {
|
|
if err != nil {
|
|
- fmt.Println("Error listing images:", err)
|
|
|
|
- return false
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 检查指定名称的镜像是否存在
|
|
|
|
- for _, imageObject := range images {
|
|
|
|
- for _, tag := range imageObject.RepoTags {
|
|
|
|
- if tag == imageName {
|
|
|
|
- return true
|
|
|
|
- }
|
|
|
|
|
|
+ if client.IsErrNotFound(err) {
|
|
|
|
+ // 镜像不存在
|
|
|
|
+ return false
|
|
}
|
|
}
|
|
|
|
+ // 处理其他错误
|
|
|
|
+ fmt.Println("Error inspecting image:", err)
|
|
|
|
+ return false
|
|
}
|
|
}
|
|
|
|
|
|
- return false
|
|
|
|
|
|
+ // 镜像存在
|
|
|
|
+ return true
|
|
}
|
|
}
|