Skip to content

Commit cfcbfab

Browse files
committed
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>
1 parent 84f5e53 commit cfcbfab

File tree

7 files changed

+24
-30
lines changed

7 files changed

+24
-30
lines changed

api/server/router/image/image_routes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ func (ir *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter,
459459
useNone := versions.LessThan(version, "1.43")
460460
withVirtualSize := versions.LessThan(version, "1.44")
461461
noDescriptor := versions.LessThan(version, "1.48")
462+
noContainers := versions.LessThan(version, "1.51")
462463
for _, img := range images {
463464
if useNone {
464465
if len(img.RepoTags) == 0 && len(img.RepoDigests) == 0 {
@@ -479,6 +480,9 @@ func (ir *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter,
479480
if noDescriptor {
480481
img.Descriptor = nil
481482
}
483+
if noContainers {
484+
img.Containers = -1
485+
}
482486
}
483487

484488
return httputils.WriteJSON(w, http.StatusOK, images)

api/swagger.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,8 +2196,7 @@ definitions:
21962196
Number of containers using this image. Includes both stopped and running
21972197
containers.
21982198
2199-
This size is not calculated by default, and depends on which API endpoint
2200-
is used. `-1` indicates that the value has not been set / calculated.
2199+
`-1` indicates that the value has not been set / calculated.
22012200
x-nullable: false
22022201
type: "integer"
22032202
example: 2

api/types/image/opts.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ type ListOptions struct {
7575
SharedSize bool
7676

7777
// ContainerCount indicates whether container count should be computed.
78+
//
79+
// Deprecated: This field has been unused and is no longer required and will be removed in a future version.
7880
ContainerCount bool
7981

8082
// Manifests indicates whether the image manifests should be returned.

daemon/containerd/image_list.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,7 @@ func (i *ImageService) imageSummary(ctx context.Context, img c8dimages.Image, pl
409409
image.Manifests = summary.Manifests
410410
target := img.Target
411411
image.Descriptor = &target
412-
413-
if opts.ContainerCount {
414-
image.Containers = summary.ContainersCount
415-
}
412+
image.Containers = summary.ContainersCount
416413
return image, summary, nil
417414
}
418415

daemon/disk_usage.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ func (daemon *Daemon) imageDiskUsage(ctx context.Context) ([]*image.Summary, err
5757
imgs, _, err := daemon.usageImages.Do(ctx, struct{}{}, func(ctx context.Context) ([]*image.Summary, error) {
5858
// Get all top images with extra attributes
5959
imgs, err := daemon.imageService.Images(ctx, image.ListOptions{
60-
Filters: filters.NewArgs(),
61-
SharedSize: true,
62-
ContainerCount: true,
60+
Filters: filters.NewArgs(),
61+
SharedSize: true,
6362
})
6463
if err != nil {
6564
return nil, errors.Wrap(err, "failed to retrieve image list")

daemon/images/image_list.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (i *ImageService) Images(ctx context.Context, opts imagetypes.ListOptions)
105105

106106
var (
107107
summaries = make([]*imagetypes.Summary, 0, len(selectedImages))
108-
summaryMap map[*image.Image]*imagetypes.Summary
108+
summaryMap = make(map[*image.Image]*imagetypes.Summary, len(selectedImages))
109109
allContainers []*container.Container
110110
)
111111
for id, img := range selectedImages {
@@ -198,30 +198,20 @@ func (i *ImageService) Images(ctx context.Context, opts imagetypes.ListOptions)
198198
continue
199199
}
200200

201-
if opts.ContainerCount {
202-
// Lazily init allContainers.
203-
if allContainers == nil {
204-
allContainers = i.containers.List()
205-
}
206-
207-
// Get container count
208-
var containers int64
209-
for _, c := range allContainers {
210-
if c.ImageID == id {
211-
containers++
212-
}
213-
}
214-
// NOTE: By default, Containers is -1, or "not set"
215-
summary.Containers = containers
201+
// Lazily init allContainers.
202+
if allContainers == nil {
203+
allContainers = i.containers.List()
216204
}
217205

218-
if opts.ContainerCount || opts.SharedSize {
219-
// Lazily init summaryMap.
220-
if summaryMap == nil {
221-
summaryMap = make(map[*image.Image]*imagetypes.Summary, len(selectedImages))
206+
// Get container count
207+
var containersCount int64
208+
for _, c := range allContainers {
209+
if c.ImageID == id {
210+
containersCount++
222211
}
223-
summaryMap[img] = summary
224212
}
213+
summary.Containers = containersCount
214+
summaryMap[img] = summary
225215
summaries = append(summaries, summary)
226216
}
227217

docs/api/version-history.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ keywords: "API, Docker, rcli, REST, documentation"
1717

1818
[Docker Engine API v1.51](https://docs.docker.com/reference/api/engine/version/v1.51/) documentation
1919

20+
* `GET /images/json` now sets the value of `Containers` field for all images
21+
to the count of containers using the image.
22+
This field was previously always -1.
2023

2124
## v1.50 API changes
2225

0 commit comments

Comments
 (0)