api/image/list: Return Containers count

This parameter was already supported for some time in the backend (for
purposes related to docker system prune). It was also already present in
the imagetypes.ListOptions but was never actually handled by the client.

Make it available by default in the response.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2024-03-05 17:26:27 +01:00
parent 84f5e5351c
commit cfcbfabb0f
7 changed files with 24 additions and 30 deletions

View File

@@ -459,6 +459,7 @@ func (ir *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter,
useNone := versions.LessThan(version, "1.43")
withVirtualSize := versions.LessThan(version, "1.44")
noDescriptor := versions.LessThan(version, "1.48")
noContainers := versions.LessThan(version, "1.51")
for _, img := range images {
if useNone {
if len(img.RepoTags) == 0 && len(img.RepoDigests) == 0 {
@@ -479,6 +480,9 @@ func (ir *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter,
if noDescriptor {
img.Descriptor = nil
}
if noContainers {
img.Containers = -1
}
}
return httputils.WriteJSON(w, http.StatusOK, images)

View File

@@ -2196,8 +2196,7 @@ definitions:
Number of containers using this image. Includes both stopped and running
containers.
This size is not calculated by default, and depends on which API endpoint
is used. `-1` indicates that the value has not been set / calculated.
`-1` indicates that the value has not been set / calculated.
x-nullable: false
type: "integer"
example: 2

View File

@@ -75,6 +75,8 @@ type ListOptions struct {
SharedSize bool
// ContainerCount indicates whether container count should be computed.
//
// Deprecated: This field has been unused and is no longer required and will be removed in a future version.
ContainerCount bool
// Manifests indicates whether the image manifests should be returned.

View File

@@ -409,10 +409,7 @@ func (i *ImageService) imageSummary(ctx context.Context, img c8dimages.Image, pl
image.Manifests = summary.Manifests
target := img.Target
image.Descriptor = &target
if opts.ContainerCount {
image.Containers = summary.ContainersCount
}
return image, summary, nil
}

View File

@@ -59,7 +59,6 @@ func (daemon *Daemon) imageDiskUsage(ctx context.Context) ([]*image.Summary, err
imgs, err := daemon.imageService.Images(ctx, image.ListOptions{
Filters: filters.NewArgs(),
SharedSize: true,
ContainerCount: true,
})
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve image list")

View File

@@ -105,7 +105,7 @@ func (i *ImageService) Images(ctx context.Context, opts imagetypes.ListOptions)
var (
summaries = make([]*imagetypes.Summary, 0, len(selectedImages))
summaryMap map[*image.Image]*imagetypes.Summary
summaryMap = make(map[*image.Image]*imagetypes.Summary, len(selectedImages))
allContainers []*container.Container
)
for id, img := range selectedImages {
@@ -198,30 +198,20 @@ func (i *ImageService) Images(ctx context.Context, opts imagetypes.ListOptions)
continue
}
if opts.ContainerCount {
// Lazily init allContainers.
if allContainers == nil {
allContainers = i.containers.List()
}
// Get container count
var containers int64
var containersCount int64
for _, c := range allContainers {
if c.ImageID == id {
containers++
containersCount++
}
}
// NOTE: By default, Containers is -1, or "not set"
summary.Containers = containers
}
if opts.ContainerCount || opts.SharedSize {
// Lazily init summaryMap.
if summaryMap == nil {
summaryMap = make(map[*image.Image]*imagetypes.Summary, len(selectedImages))
}
summary.Containers = containersCount
summaryMap[img] = summary
}
summaries = append(summaries, summary)
}

View File

@@ -17,6 +17,9 @@ keywords: "API, Docker, rcli, REST, documentation"
[Docker Engine API v1.51](https://docs.docker.com/reference/api/engine/version/v1.51/) documentation
* `GET /images/json` now sets the value of `Containers` field for all images
to the count of containers using the image.
This field was previously always -1.
## v1.50 API changes