2018-02-07 15:52:47 -05:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2018-02-21 17:10:18 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2018-02-07 15:52:47 -05:00
|
|
|
"fmt"
|
API: add "prune" events
This patch adds a new "prune" event type to indicate that pruning of a resource
type completed.
This event-type can be used on systems that want to perform actions after
resources have been cleaned up. For example, Docker Desktop performs an fstrim
after resources are deleted (https://github.com/linuxkit/linuxkit/tree/v0.7/pkg/trim-after-delete).
While the current (remove, destroy) events can provide information on _most_
resources, there is currently no event triggered after the BuildKit build-cache
is cleaned.
Prune events have a `reclaimed` attribute, indicating the amount of space that
was reclaimed (in bytes). The attribute can be used, for example, to use as a
threshold for performing fstrim actions. Reclaimed space for `network` events
will always be 0, but the field is added to be consistent with prune events for
other resources.
To test this patch:
Create some resources:
for i in foo bar baz; do \
docker network create network_$i \
&& docker volume create volume_$i \
&& docker run -d --name container_$i -v volume_$i:/volume busybox sh -c 'truncate -s 5M somefile; truncate -s 5M /volume/file' \
&& docker tag busybox:latest image_$i; \
done;
docker pull alpine
docker pull nginx:alpine
echo -e "FROM busybox\nRUN truncate -s 50M bigfile" | DOCKER_BUILDKIT=1 docker build -
Start listening for "prune" events in another shell:
docker events --filter event=prune
Prune containers, networks, volumes, and build-cache:
docker system prune -af --volumes
See the events that are returned:
docker events --filter event=prune
2020-07-25T12:12:09.268491000Z container prune (reclaimed=15728640)
2020-07-25T12:12:09.447890400Z network prune (reclaimed=0)
2020-07-25T12:12:09.452323000Z volume prune (reclaimed=15728640)
2020-07-25T12:12:09.517236200Z image prune (reclaimed=21568540)
2020-07-25T12:12:09.566662600Z builder prune (reclaimed=52428841)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-07-25 08:14:38 -04:00
|
|
|
"strconv"
|
2018-02-21 17:10:18 -05:00
|
|
|
"sync/atomic"
|
2018-02-07 15:52:47 -05:00
|
|
|
"time"
|
2018-02-21 17:10:18 -05:00
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/docker/docker/api/types"
|
API: add "prune" events
This patch adds a new "prune" event type to indicate that pruning of a resource
type completed.
This event-type can be used on systems that want to perform actions after
resources have been cleaned up. For example, Docker Desktop performs an fstrim
after resources are deleted (https://github.com/linuxkit/linuxkit/tree/v0.7/pkg/trim-after-delete).
While the current (remove, destroy) events can provide information on _most_
resources, there is currently no event triggered after the BuildKit build-cache
is cleaned.
Prune events have a `reclaimed` attribute, indicating the amount of space that
was reclaimed (in bytes). The attribute can be used, for example, to use as a
threshold for performing fstrim actions. Reclaimed space for `network` events
will always be 0, but the field is added to be consistent with prune events for
other resources.
To test this patch:
Create some resources:
for i in foo bar baz; do \
docker network create network_$i \
&& docker volume create volume_$i \
&& docker run -d --name container_$i -v volume_$i:/volume busybox sh -c 'truncate -s 5M somefile; truncate -s 5M /volume/file' \
&& docker tag busybox:latest image_$i; \
done;
docker pull alpine
docker pull nginx:alpine
echo -e "FROM busybox\nRUN truncate -s 50M bigfile" | DOCKER_BUILDKIT=1 docker build -
Start listening for "prune" events in another shell:
docker events --filter event=prune
Prune containers, networks, volumes, and build-cache:
docker system prune -af --volumes
See the events that are returned:
docker events --filter event=prune
2020-07-25T12:12:09.268491000Z container prune (reclaimed=15728640)
2020-07-25T12:12:09.447890400Z network prune (reclaimed=0)
2020-07-25T12:12:09.452323000Z volume prune (reclaimed=15728640)
2020-07-25T12:12:09.517236200Z image prune (reclaimed=21568540)
2020-07-25T12:12:09.566662600Z builder prune (reclaimed=52428841)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-07-25 08:14:38 -04:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2018-02-21 17:10:18 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-02-07 15:52:47 -05:00
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2018-02-21 17:10:18 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2018-03-22 17:11:03 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-02-21 17:10:18 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var imagesAcceptedFilters = map[string]bool{
|
|
|
|
"dangling": true,
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
"until": true,
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:52:47 -05:00
|
|
|
// errPruneRunning is returned when a prune request is received while
|
|
|
|
// one is in progress
|
2018-03-22 17:11:03 -04:00
|
|
|
var errPruneRunning = errdefs.Conflict(errors.New("a prune operation is already running"))
|
2018-02-07 15:52:47 -05:00
|
|
|
|
2018-02-21 17:10:18 -05:00
|
|
|
// ImagesPrune removes unused images
|
2018-02-07 15:52:47 -05:00
|
|
|
func (i *ImageService) ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error) {
|
2018-02-02 17:18:46 -05:00
|
|
|
if !atomic.CompareAndSwapInt32(&i.pruneRunning, 0, 1) {
|
2018-02-21 17:10:18 -05:00
|
|
|
return nil, errPruneRunning
|
|
|
|
}
|
2018-02-02 17:18:46 -05:00
|
|
|
defer atomic.StoreInt32(&i.pruneRunning, 0)
|
2018-02-21 17:10:18 -05:00
|
|
|
|
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(imagesAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rep := &types.ImagesPruneReport{}
|
|
|
|
|
|
|
|
danglingOnly := true
|
|
|
|
if pruneFilters.Contains("dangling") {
|
|
|
|
if pruneFilters.ExactMatch("dangling", "false") || pruneFilters.ExactMatch("dangling", "0") {
|
|
|
|
danglingOnly = false
|
|
|
|
} else if !pruneFilters.ExactMatch("dangling", "true") && !pruneFilters.ExactMatch("dangling", "1") {
|
|
|
|
return nil, invalidFilter{"dangling", pruneFilters.Get("dangling")}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
until, err := getUntilFromPruneFilters(pruneFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var allImages map[image.ID]*image.Image
|
|
|
|
if danglingOnly {
|
2018-02-02 17:18:46 -05:00
|
|
|
allImages = i.imageStore.Heads()
|
2018-02-21 17:10:18 -05:00
|
|
|
} else {
|
2018-02-02 17:18:46 -05:00
|
|
|
allImages = i.imageStore.Map()
|
2018-02-21 17:10:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter intermediary images and get their unique size
|
2021-03-19 10:34:08 -04:00
|
|
|
allLayers := i.layerStore.Map()
|
2018-02-21 17:10:18 -05:00
|
|
|
topImages := map[image.ID]*image.Image{}
|
|
|
|
for id, img := range allImages {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
dgst := digest.Digest(id)
|
2018-02-02 17:18:46 -05:00
|
|
|
if len(i.referenceStore.References(dgst)) == 0 && len(i.imageStore.Children(id)) != 0 {
|
2018-02-21 17:10:18 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !until.IsZero() && img.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if img.Config != nil && !matchLabels(pruneFilters, img.Config.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
topImages[id] = img
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canceled := false
|
|
|
|
deleteImagesLoop:
|
|
|
|
for id := range topImages {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// we still want to calculate freed size and return the data
|
|
|
|
canceled = true
|
|
|
|
break deleteImagesLoop
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
deletedImages := []types.ImageDeleteResponseItem{}
|
2018-02-07 15:52:47 -05:00
|
|
|
refs := i.referenceStore.References(id.Digest())
|
2018-02-21 17:10:18 -05:00
|
|
|
if len(refs) > 0 {
|
|
|
|
shouldDelete := !danglingOnly
|
|
|
|
if !shouldDelete {
|
|
|
|
hasTag := false
|
|
|
|
for _, ref := range refs {
|
|
|
|
if _, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
hasTag = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only delete if it's untagged (i.e. repo:<none>)
|
|
|
|
shouldDelete = !hasTag
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldDelete {
|
|
|
|
for _, ref := range refs {
|
2018-02-02 17:18:46 -05:00
|
|
|
imgDel, err := i.ImageDelete(ref.String(), false, true)
|
2018-02-21 17:10:18 -05:00
|
|
|
if imageDeleteFailed(ref.String(), err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deletedImages = append(deletedImages, imgDel...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hex := id.Digest().Hex()
|
2018-02-02 17:18:46 -05:00
|
|
|
imgDel, err := i.ImageDelete(hex, false, true)
|
2018-02-21 17:10:18 -05:00
|
|
|
if imageDeleteFailed(hex, err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deletedImages = append(deletedImages, imgDel...)
|
|
|
|
}
|
|
|
|
|
|
|
|
rep.ImagesDeleted = append(rep.ImagesDeleted, deletedImages...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute how much space was freed
|
|
|
|
for _, d := range rep.ImagesDeleted {
|
|
|
|
if d.Deleted != "" {
|
|
|
|
chid := layer.ChainID(d.Deleted)
|
|
|
|
if l, ok := allLayers[chid]; ok {
|
2022-01-21 13:01:34 -05:00
|
|
|
rep.SpaceReclaimed += uint64(l.DiffSize())
|
2018-02-21 17:10:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if canceled {
|
|
|
|
logrus.Debugf("ImagesPrune operation cancelled: %#v", *rep)
|
|
|
|
}
|
API: add "prune" events
This patch adds a new "prune" event type to indicate that pruning of a resource
type completed.
This event-type can be used on systems that want to perform actions after
resources have been cleaned up. For example, Docker Desktop performs an fstrim
after resources are deleted (https://github.com/linuxkit/linuxkit/tree/v0.7/pkg/trim-after-delete).
While the current (remove, destroy) events can provide information on _most_
resources, there is currently no event triggered after the BuildKit build-cache
is cleaned.
Prune events have a `reclaimed` attribute, indicating the amount of space that
was reclaimed (in bytes). The attribute can be used, for example, to use as a
threshold for performing fstrim actions. Reclaimed space for `network` events
will always be 0, but the field is added to be consistent with prune events for
other resources.
To test this patch:
Create some resources:
for i in foo bar baz; do \
docker network create network_$i \
&& docker volume create volume_$i \
&& docker run -d --name container_$i -v volume_$i:/volume busybox sh -c 'truncate -s 5M somefile; truncate -s 5M /volume/file' \
&& docker tag busybox:latest image_$i; \
done;
docker pull alpine
docker pull nginx:alpine
echo -e "FROM busybox\nRUN truncate -s 50M bigfile" | DOCKER_BUILDKIT=1 docker build -
Start listening for "prune" events in another shell:
docker events --filter event=prune
Prune containers, networks, volumes, and build-cache:
docker system prune -af --volumes
See the events that are returned:
docker events --filter event=prune
2020-07-25T12:12:09.268491000Z container prune (reclaimed=15728640)
2020-07-25T12:12:09.447890400Z network prune (reclaimed=0)
2020-07-25T12:12:09.452323000Z volume prune (reclaimed=15728640)
2020-07-25T12:12:09.517236200Z image prune (reclaimed=21568540)
2020-07-25T12:12:09.566662600Z builder prune (reclaimed=52428841)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-07-25 08:14:38 -04:00
|
|
|
i.eventsService.Log("prune", events.ImageEventType, events.Actor{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"reclaimed": strconv.FormatUint(rep.SpaceReclaimed, 10),
|
|
|
|
},
|
|
|
|
})
|
2018-02-21 17:10:18 -05:00
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageDeleteFailed(ref string, err error) bool {
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return false
|
|
|
|
case errdefs.IsConflict(err):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
logrus.Warnf("failed to prune image %s: %v", ref, err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 15:52:47 -05:00
|
|
|
|
|
|
|
func matchLabels(pruneFilters filters.Args, labels map[string]string) bool {
|
|
|
|
if !pruneFilters.MatchKVList("label", labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// By default MatchKVList will return true if field (like 'label!') does not exist
|
|
|
|
// So we have to add additional Contains("label!") check
|
|
|
|
if pruneFilters.Contains("label!") {
|
|
|
|
if pruneFilters.MatchKVList("label!", labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUntilFromPruneFilters(pruneFilters filters.Args) (time.Time, error) {
|
|
|
|
until := time.Time{}
|
|
|
|
if !pruneFilters.Contains("until") {
|
|
|
|
return until, nil
|
|
|
|
}
|
|
|
|
untilFilters := pruneFilters.Get("until")
|
|
|
|
if len(untilFilters) > 1 {
|
|
|
|
return until, fmt.Errorf("more than one until filter specified")
|
|
|
|
}
|
|
|
|
ts, err := timetypes.GetTimestamp(untilFilters[0], time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return until, err
|
|
|
|
}
|
|
|
|
seconds, nanoseconds, err := timetypes.ParseTimestamps(ts, 0)
|
|
|
|
if err != nil {
|
|
|
|
return until, err
|
|
|
|
}
|
|
|
|
until = time.Unix(seconds, nanoseconds)
|
|
|
|
return until, nil
|
|
|
|
}
|