2018-02-07 15:52:47 -05:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2015-11-18 17:20:54 -05:00
|
|
|
|
|
|
|
import (
|
2021-06-22 13:09:58 -04:00
|
|
|
"context"
|
2015-11-18 17:20:54 -05:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2016-11-11 09:34:01 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2022-07-25 11:24:03 -04:00
|
|
|
imagetypes "github.com/docker/docker/api/types/image"
|
2016-08-23 19:19:37 -04:00
|
|
|
"github.com/docker/docker/container"
|
2015-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2017-11-20 11:33:20 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-11-18 17:20:54 -05:00
|
|
|
)
|
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
var acceptedImageFilterTags = map[string]bool{
|
2016-11-11 09:34:01 -05:00
|
|
|
"dangling": true,
|
|
|
|
"label": true,
|
|
|
|
"before": true,
|
|
|
|
"since": true,
|
|
|
|
"reference": true,
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// byCreated is a temporary type used to sort a list of images by creation
|
|
|
|
// time.
|
2016-10-03 15:17:39 -04:00
|
|
|
type byCreated []*types.ImageSummary
|
2015-11-18 17:20:54 -05:00
|
|
|
|
|
|
|
func (r byCreated) Len() int { return len(r) }
|
|
|
|
func (r byCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
|
|
|
|
|
2021-06-22 13:09:58 -04:00
|
|
|
// Images returns a filtered list of images.
|
2022-08-09 08:42:50 -04:00
|
|
|
func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error) {
|
2021-06-22 13:09:58 -04:00
|
|
|
if err := opts.Filters.Validate(acceptedImageFilterTags); err != nil {
|
2015-11-25 20:27:11 -05:00
|
|
|
return nil, err
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
var danglingOnly bool
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.Filters.Contains("dangling") {
|
|
|
|
if opts.Filters.ExactMatch("dangling", "true") {
|
2015-11-25 20:27:11 -05:00
|
|
|
danglingOnly = true
|
2021-06-22 13:09:58 -04:00
|
|
|
} else if !opts.Filters.ExactMatch("dangling", "false") {
|
|
|
|
return nil, invalidFilter{"dangling", opts.Filters.Get("dangling")}
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
var (
|
|
|
|
beforeFilter, sinceFilter *image.Image
|
|
|
|
err error
|
|
|
|
)
|
2021-06-22 13:09:58 -04:00
|
|
|
err = opts.Filters.WalkValues("before", func(value string) error {
|
2022-08-09 08:42:50 -04:00
|
|
|
beforeFilter, err = i.GetImage(ctx, value, imagetypes.GetImageOpts{})
|
2016-05-25 07:49:10 -04:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:09:58 -04:00
|
|
|
err = opts.Filters.WalkValues("since", func(value string) error {
|
2022-08-09 08:42:50 -04:00
|
|
|
sinceFilter, err = i.GetImage(ctx, value, imagetypes.GetImageOpts{})
|
2016-05-25 07:49:10 -04:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:04:59 -04:00
|
|
|
var selectedImages map[image.ID]*image.Image
|
2021-06-22 09:30:46 -04:00
|
|
|
if danglingOnly {
|
2021-06-29 06:04:59 -04:00
|
|
|
selectedImages = i.imageStore.Heads()
|
2021-06-22 09:30:46 -04:00
|
|
|
} else {
|
2021-06-29 06:04:59 -04:00
|
|
|
selectedImages = i.imageStore.Map()
|
2021-06-22 09:30:46 -04:00
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
var (
|
2021-06-29 06:04:59 -04:00
|
|
|
summaries = make([]*types.ImageSummary, 0, len(selectedImages))
|
2021-06-22 09:30:46 -04:00
|
|
|
summaryMap map[*image.Image]*types.ImageSummary
|
|
|
|
allContainers []*container.Container
|
|
|
|
)
|
2021-06-29 06:04:59 -04:00
|
|
|
for id, img := range selectedImages {
|
2016-05-25 07:49:10 -04:00
|
|
|
if beforeFilter != nil {
|
|
|
|
if img.Created.Equal(beforeFilter.Created) || img.Created.After(beforeFilter.Created) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinceFilter != nil {
|
|
|
|
if img.Created.Equal(sinceFilter.Created) || img.Created.Before(sinceFilter.Created) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.Filters.Contains("label") {
|
2015-11-25 20:27:11 -05:00
|
|
|
// Very old image that do not have image.Config (or even labels)
|
2015-11-18 17:20:54 -05:00
|
|
|
if img.Config == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We are now sure image.Config is not nil
|
2021-06-22 13:09:58 -04:00
|
|
|
if !opts.Filters.MatchKVList("label", img.Config.Labels) {
|
2015-11-18 17:20:54 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 11:33:20 -05:00
|
|
|
// Skip any images with an unsupported operating system to avoid a potential
|
|
|
|
// panic when indexing through the layerstore. Don't error as we want to list
|
|
|
|
// the other images. This should never happen, but here as a safety precaution.
|
|
|
|
if !system.IsOSSupported(img.OperatingSystem()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
var size int64
|
2021-06-22 09:30:46 -04:00
|
|
|
if layerID := img.RootFS.ChainID(); layerID != "" {
|
2021-03-19 10:34:08 -04:00
|
|
|
l, err := i.layerStore.Get(layerID)
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
2017-03-15 10:14:14 -04:00
|
|
|
// The layer may have been deleted between the call to `Map()` or
|
|
|
|
// `Heads()` and the call to `Get()`, so we just ignore this error
|
|
|
|
if err == layer.ErrLayerDoesNotExist {
|
|
|
|
continue
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-21 13:01:34 -05:00
|
|
|
size = l.Size()
|
2021-03-19 10:34:08 -04:00
|
|
|
layer.ReleaseAndLog(i.layerStore, l)
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
summary := newImageSummary(img, size)
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2018-02-02 17:18:46 -05:00
|
|
|
for _, ref := range i.referenceStore.References(id.Digest()) {
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.Filters.Contains("reference") {
|
2016-11-11 09:34:01 -05:00
|
|
|
var found bool
|
|
|
|
var matchErr error
|
2021-06-22 13:09:58 -04:00
|
|
|
for _, pattern := range opts.Filters.Get("reference") {
|
2017-01-25 19:54:18 -05:00
|
|
|
found, matchErr = reference.FamiliarMatch(pattern, ref)
|
2016-11-11 09:34:01 -05:00
|
|
|
if matchErr != nil {
|
|
|
|
return nil, matchErr
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
2018-11-09 22:00:26 -05:00
|
|
|
if found {
|
|
|
|
break
|
|
|
|
}
|
2016-11-11 09:34:01 -05:00
|
|
|
}
|
|
|
|
if !found {
|
2015-11-18 17:20:54 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-12-04 16:55:15 -05:00
|
|
|
if _, ok := ref.(reference.Canonical); ok {
|
2021-06-22 09:30:46 -04:00
|
|
|
summary.RepoDigests = append(summary.RepoDigests, reference.FamiliarString(ref))
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
2015-12-04 16:55:15 -05:00
|
|
|
if _, ok := ref.(reference.NamedTagged); ok {
|
2021-06-22 09:30:46 -04:00
|
|
|
summary.RepoTags = append(summary.RepoTags, reference.FamiliarString(ref))
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-22 09:30:46 -04:00
|
|
|
if summary.RepoDigests == nil && summary.RepoTags == nil {
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.All || len(i.imageStore.Children(id)) == 0 {
|
|
|
|
if opts.Filters.Contains("dangling") && !danglingOnly {
|
2019-11-27 09:43:53 -05:00
|
|
|
// dangling=false case, so dangling image is not needed
|
2016-01-14 01:58:31 -05:00
|
|
|
continue
|
|
|
|
}
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.Filters.Contains("reference") { // skip images with no references if filtering by reference
|
2015-11-18 17:20:54 -05:00
|
|
|
continue
|
|
|
|
}
|
2021-06-22 09:30:46 -04:00
|
|
|
summary.RepoDigests = []string{"<none>@<none>"}
|
|
|
|
summary.RepoTags = []string{"<none>:<none>"}
|
2015-11-18 17:20:54 -05:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-22 09:30:46 -04:00
|
|
|
} else if danglingOnly && len(summary.RepoTags) > 0 {
|
2015-11-18 17:20:54 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.ContainerCount {
|
|
|
|
// Lazily init allContainers.
|
|
|
|
if allContainers == nil {
|
2018-02-02 17:18:46 -05:00
|
|
|
allContainers = i.containers.List()
|
2016-08-23 19:19:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get container count
|
2021-06-22 09:30:46 -04:00
|
|
|
var containers int64
|
2016-08-23 19:19:37 -04:00
|
|
|
for _, c := range allContainers {
|
|
|
|
if c.ImageID == id {
|
2021-06-22 09:30:46 -04:00
|
|
|
containers++
|
2016-08-23 19:19:37 -04:00
|
|
|
}
|
|
|
|
}
|
2021-06-22 09:30:46 -04:00
|
|
|
// NOTE: By default, Containers is -1, or "not set"
|
|
|
|
summary.Containers = containers
|
2021-06-22 13:09:58 -04:00
|
|
|
}
|
2016-08-23 19:19:37 -04:00
|
|
|
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.ContainerCount || opts.SharedSize {
|
|
|
|
// Lazily init summaryMap.
|
|
|
|
if summaryMap == nil {
|
|
|
|
summaryMap = make(map[*image.Image]*types.ImageSummary, len(selectedImages))
|
|
|
|
}
|
2021-06-29 06:04:59 -04:00
|
|
|
summaryMap[img] = summary
|
|
|
|
}
|
|
|
|
summaries = append(summaries, summary)
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:09:58 -04:00
|
|
|
if opts.SharedSize {
|
2021-06-29 06:04:59 -04:00
|
|
|
allLayers := i.layerStore.Map()
|
|
|
|
layerRefs := make(map[layer.ChainID]int, len(allLayers))
|
|
|
|
|
|
|
|
allImages := selectedImages
|
|
|
|
if danglingOnly {
|
|
|
|
// If danglingOnly is true, then selectedImages include only dangling images,
|
|
|
|
// but we need to consider all existing images to correctly perform reference counting.
|
|
|
|
// If danglingOnly is false, selectedImages (and, hence, allImages) is already equal to i.imageStore.Map()
|
|
|
|
// and we can avoid performing an otherwise redundant method call.
|
|
|
|
allImages = i.imageStore.Map()
|
|
|
|
}
|
|
|
|
// Count layer references across all known images
|
|
|
|
for _, img := range allImages {
|
2016-08-23 19:19:37 -04:00
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = nil
|
|
|
|
for _, id := range img.RootFS.DiffIDs {
|
|
|
|
rootFS.Append(id)
|
2021-06-22 09:30:46 -04:00
|
|
|
layerRefs[rootFS.ChainID()]++
|
2016-08-23 19:19:37 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2017-01-23 16:52:33 -05:00
|
|
|
// Get Shared sizes
|
2021-06-22 09:30:46 -04:00
|
|
|
for img, summary := range summaryMap {
|
2016-08-23 19:19:37 -04:00
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = nil
|
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
// Indicate that we collected shared size information (default is -1, or "not set")
|
|
|
|
summary.SharedSize = 0
|
2016-08-23 19:19:37 -04:00
|
|
|
for _, id := range img.RootFS.DiffIDs {
|
|
|
|
rootFS.Append(id)
|
|
|
|
chid := rootFS.ChainID()
|
|
|
|
|
|
|
|
if layerRefs[chid] > 1 {
|
2021-06-22 09:30:46 -04:00
|
|
|
if _, ok := allLayers[chid]; !ok {
|
|
|
|
return nil, fmt.Errorf("layer %v was not found (corruption?)", chid)
|
|
|
|
}
|
2022-01-21 13:01:34 -05:00
|
|
|
summary.SharedSize += allLayers[chid].DiffSize()
|
2016-08-23 19:19:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
sort.Sort(sort.Reverse(byCreated(summaries)))
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
return summaries, nil
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
|
|
|
|
2021-06-22 09:30:46 -04:00
|
|
|
func newImageSummary(image *image.Image, size int64) *types.ImageSummary {
|
|
|
|
summary := &types.ImageSummary{
|
|
|
|
ParentID: image.Parent.String(),
|
|
|
|
ID: image.ID().String(),
|
|
|
|
Created: image.Created.Unix(),
|
|
|
|
Size: size,
|
|
|
|
VirtualSize: size,
|
|
|
|
// -1 indicates that the value has not been set (avoids ambiguity
|
|
|
|
// between 0 (default) and "not set". We cannot use a pointer (nil)
|
|
|
|
// for this, as the JSON representation uses "omitempty", which would
|
|
|
|
// consider both "0" and "nil" to be "empty".
|
|
|
|
SharedSize: -1,
|
|
|
|
Containers: -1,
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
if image.Config != nil {
|
2021-06-22 09:30:46 -04:00
|
|
|
summary.Labels = image.Config.Labels
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
2021-06-22 09:30:46 -04:00
|
|
|
return summary
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|