LingxinMeng 7 tháng trước cách đây
mục cha
commit
0e0e732c05
1 tập tin đã thay đổi với 10 bổ sung14 xóa
  1. 10 14
      src/package/util/u_docker.go

+ 10 - 14
src/package/util/u_docker.go

@@ -3,7 +3,6 @@ package util
 import (
 	"context"
 	"fmt"
-	"github.com/docker/docker/api/types/image"
 	"github.com/docker/docker/client"
 )
 
@@ -17,21 +16,18 @@ func ImageExists(cli *client.Client, imageName string) bool {
 	// 设置上下文
 	ctx := context.Background()
 
-	// 获取所有的镜像
-	images, err := cli.ImageList(ctx, image.ListOptions{})
+	// 检查镜像是否存在
+	_, _, err := cli.ImageInspectWithRaw(ctx, imageName)
 	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
 }