2024-01-08 20:26:03 +01:00
|
|
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
2023-01-10 23:08:57 +01:00
|
|
|
|
2019-02-17 21:40:22 -08:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-29 06:08:40 +02:00
|
|
|
"fmt"
|
2019-02-17 21:40:22 -08:00
|
|
|
|
2025-07-26 03:55:31 +00:00
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
2025-06-11 14:57:23 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2019-02-17 21:40:22 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
2021-03-29 06:08:40 +02:00
|
|
|
// requested name, tag and architecture exists in the local docker image store
|
2025-07-28 12:26:41 +00:00
|
|
|
func ImageExistsLocally(ctx context.Context, imageName, platform string) (bool, error) {
|
2020-05-04 14:15:42 +10:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-02-17 21:40:22 -08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-10-24 18:50:43 +02:00
|
|
|
defer cli.Close()
|
2019-02-17 21:40:22 -08:00
|
|
|
|
2025-07-26 03:55:31 +00:00
|
|
|
inspectImage, err := cli.ImageInspect(ctx, imageName)
|
|
|
|
if cerrdefs.IsNotFound(err) {
|
2023-01-13 18:52:54 +01:00
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2019-02-17 21:40:22 -08:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:52:54 +01:00
|
|
|
if platform == "" || platform == "any" || fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
|
|
|
return true, nil
|
2021-03-29 06:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 17:15:13 +02:00
|
|
|
// RemoveImage removes image from local store, the function is used to run different
|
2021-03-29 06:08:40 +02:00
|
|
|
// container image architectures
|
2025-07-28 12:26:41 +00:00
|
|
|
func RemoveImage(ctx context.Context, imageName string, force, pruneChildren bool) (bool, error) {
|
2021-03-29 06:08:40 +02:00
|
|
|
cli, err := GetDockerClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-01-13 18:52:54 +01:00
|
|
|
defer cli.Close()
|
2021-03-29 06:08:40 +02:00
|
|
|
|
2025-06-11 14:57:23 +00:00
|
|
|
inspectImage, err := cli.ImageInspect(ctx, imageName)
|
2025-07-26 03:55:31 +00:00
|
|
|
if cerrdefs.IsNotFound(err) {
|
2023-01-13 18:52:54 +01:00
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2021-03-29 06:08:40 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2025-06-11 14:57:23 +00:00
|
|
|
if _, err = cli.ImageRemove(ctx, inspectImage.ID, image.RemoveOptions{
|
2023-01-13 18:52:54 +01:00
|
|
|
Force: force,
|
|
|
|
PruneChildren: pruneChildren,
|
|
|
|
}); err != nil {
|
|
|
|
return false, err
|
2021-03-29 06:08:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 18:52:54 +01:00
|
|
|
return true, nil
|
2019-02-17 21:40:22 -08:00
|
|
|
}
|