Skip to content

Modern Docker Management, Designed for Everyone

864 B
Raw Blame
package imageref

import (
	"cmp"
	"context"

	"github.com/moby/moby/api/types/container"
	"github.com/moby/moby/client"
	"go.getarcane.app/docker/compat"
	kit "go.getarcane.app/kit/pkg"
	"go.getarcane.app/updater/refs"
)

// ConfiguredImage returns the image a container was created from. Docker lists
// a container whose image lost its tag by image ID, so the configured image is
// inspected only then; the listed name is the fallback.
func ConfiguredImage(ctx context.Context, dockerClient *client.Client, summary container.Summary) string {
	if !refs.IsImageIDLikeReference(summary.Image) {
		return summary.Image
	}
	result, err := compat.ContainerInspectWithCompatibility(ctx, dockerClient, summary.ID, client.ContainerInspectOptions{})
	if err != nil {
		return summary.Image
	}
	return cmp.Or(kit.FromPtr(result.Container.Config).Image, summary.Image)
}