2014-08-05 10:41:30 +04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2015-04-03 18:31:30 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-09-11 01:01:18 +03:00
|
|
|
"github.com/docker/docker/context"
|
2015-09-18 20:48:16 +03:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-07-30 02:45:47 +03:00
|
|
|
"github.com/docker/docker/graph/tags"
|
2015-07-20 20:57:15 +03:00
|
|
|
"github.com/docker/docker/image"
|
2014-08-05 10:41:30 +04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2015-03-24 14:25:26 +03:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-02-27 05:23:50 +03:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-05 10:41:30 +04:00
|
|
|
)
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// ImageDelete deletes the image referenced by the given imageRef from this
|
|
|
|
// daemon. The given imageRef can be an image ID, ID prefix, or a repository
|
|
|
|
// reference (with an optional tag or digest, defaulting to the tag name
|
|
|
|
// "latest"). There is differing behavior depending on whether the given
|
|
|
|
// imageRef is a repository reference or not.
|
|
|
|
//
|
|
|
|
// If the given imageRef is a repository reference then that repository
|
|
|
|
// reference will be removed. However, if there exists any containers which
|
|
|
|
// were created using the same image reference then the repository reference
|
|
|
|
// cannot be removed unless either there are other repository references to the
|
|
|
|
// same image or force is true. Following removal of the repository reference,
|
|
|
|
// the referenced image itself will attempt to be deleted as described below
|
|
|
|
// but quietly, meaning any image delete conflicts will cause the image to not
|
|
|
|
// be deleted and the conflict will not be reported.
|
|
|
|
//
|
|
|
|
// There may be conflicts preventing deletion of an image and these conflicts
|
|
|
|
// are divided into two categories grouped by their severity:
|
|
|
|
//
|
|
|
|
// Hard Conflict:
|
|
|
|
// - a pull or build using the image.
|
|
|
|
// - any descendent image.
|
|
|
|
// - any running container using the image.
|
|
|
|
//
|
|
|
|
// Soft Conflict:
|
|
|
|
// - any stopped container using the image.
|
|
|
|
// - any repository tag or digest references to the image.
|
|
|
|
//
|
|
|
|
// The image cannot be removed if there are any hard conflicts and can be
|
|
|
|
// removed if there are soft conflicts only if force is true.
|
|
|
|
//
|
|
|
|
// If prune is true, ancestor images will each attempt to be deleted quietly,
|
|
|
|
// meaning any delete conflicts will cause the image to not be deleted and the
|
|
|
|
// conflict will not be reported.
|
|
|
|
//
|
|
|
|
// FIXME: remove ImageDelete's dependency on Daemon, then move to the graph
|
|
|
|
// package. This would require that we no longer need the daemon to determine
|
|
|
|
// whether images are being used by a stopped or running container.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) ImageDelete(ctx context.Context, imageRef string, force, prune bool) ([]types.ImageDelete, error) {
|
2015-08-15 10:30:25 +03:00
|
|
|
records := []types.ImageDelete{}
|
|
|
|
|
2015-09-29 20:40:17 +03:00
|
|
|
img, err := daemon.Repositories(ctx).LookupImage(imageRef)
|
2015-08-15 10:30:25 +03:00
|
|
|
if err != nil {
|
2015-04-09 14:59:50 +03:00
|
|
|
return nil, err
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
2015-08-15 10:30:25 +03:00
|
|
|
|
|
|
|
var removedRepositoryRef bool
|
|
|
|
if !isImageIDPrefix(img.ID, imageRef) {
|
|
|
|
// A repository reference was given and should be removed
|
|
|
|
// first. We can only remove this reference if either force is
|
|
|
|
// true, there are multiple repository references to this
|
|
|
|
// image, or there are no containers using the given reference.
|
2015-09-11 01:01:18 +03:00
|
|
|
if !(force || daemon.imageHasMultipleRepositoryReferences(ctx, img.ID)) {
|
2015-09-29 20:40:17 +03:00
|
|
|
if container := daemon.getContainerUsingImage(ctx, img.ID); container != nil {
|
2015-08-15 10:30:25 +03:00
|
|
|
// If we removed the repository reference then
|
|
|
|
// this image would remain "dangling" and since
|
|
|
|
// we really want to avoid that the client must
|
|
|
|
// explicitly force its removal.
|
2015-09-18 20:48:16 +03:00
|
|
|
return nil, derr.ErrorCodeImgDelUsed.WithArgs(imageRef, stringid.TruncateID(container.ID), stringid.TruncateID(img.ID))
|
2015-08-15 10:30:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
parsedRef, err := daemon.removeImageRef(ctx, imageRef)
|
2015-08-15 10:30:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
untaggedRecord := types.ImageDelete{Untagged: parsedRef}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
daemon.EventsService.Log(ctx, "untag", img.ID, "")
|
2015-08-15 10:30:25 +03:00
|
|
|
records = append(records, untaggedRecord)
|
|
|
|
|
|
|
|
removedRepositoryRef = true
|
|
|
|
} else {
|
|
|
|
// If an ID reference was given AND there is exactly one
|
|
|
|
// repository reference to the image then we will want to
|
|
|
|
// remove that reference.
|
|
|
|
// FIXME: Is this the behavior we want?
|
2015-09-29 20:40:17 +03:00
|
|
|
repoRefs := daemon.Repositories(ctx).ByID()[img.ID]
|
2015-08-15 10:30:25 +03:00
|
|
|
if len(repoRefs) == 1 {
|
2015-09-11 01:01:18 +03:00
|
|
|
parsedRef, err := daemon.removeImageRef(ctx, repoRefs[0])
|
2015-08-15 10:30:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
untaggedRecord := types.ImageDelete{Untagged: parsedRef}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
daemon.EventsService.Log(ctx, "untag", img.ID, "")
|
2015-08-15 10:30:25 +03:00
|
|
|
records = append(records, untaggedRecord)
|
|
|
|
}
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
2015-04-09 14:59:50 +03:00
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
return records, daemon.imageDeleteHelper(ctx, img, &records, force, prune, removedRepositoryRef)
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// isImageIDPrefix returns whether the given possiblePrefix is a prefix of the
|
|
|
|
// given imageID.
|
|
|
|
func isImageIDPrefix(imageID, possiblePrefix string) bool {
|
|
|
|
return strings.HasPrefix(imageID, possiblePrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// imageHasMultipleRepositoryReferences returns whether there are multiple
|
|
|
|
// repository references to the given imageID.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) imageHasMultipleRepositoryReferences(ctx context.Context, imageID string) bool {
|
2015-09-29 20:40:17 +03:00
|
|
|
return len(daemon.Repositories(ctx).ByID()[imageID]) > 1
|
2015-08-15 10:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// getContainerUsingImage returns a container that was created using the given
|
|
|
|
// imageID. Returns nil if there is no such container.
|
2015-09-29 20:40:17 +03:00
|
|
|
func (daemon *Daemon) getContainerUsingImage(ctx context.Context, imageID string) *Container {
|
|
|
|
for _, container := range daemon.List(ctx) {
|
2015-08-15 10:30:25 +03:00
|
|
|
if container.ImageID == imageID {
|
|
|
|
return container
|
|
|
|
}
|
2015-08-05 18:46:57 +03:00
|
|
|
}
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-05 10:41:30 +04:00
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// removeImageRef attempts to parse and remove the given image reference from
|
|
|
|
// this daemon's store of repository tag/digest references. The given
|
|
|
|
// repositoryRef must not be an image ID but a repository name followed by an
|
|
|
|
// optional tag or digest reference. If tag or digest is omitted, the default
|
|
|
|
// tag is used. Returns the resolved image reference and an error.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) removeImageRef(ctx context.Context, repositoryRef string) (string, error) {
|
2015-08-15 10:30:25 +03:00
|
|
|
repository, ref := parsers.ParseRepositoryTag(repositoryRef)
|
|
|
|
if ref == "" {
|
|
|
|
ref = tags.DefaultTag
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// Ignore the boolean value returned, as far as we're concerned, this
|
|
|
|
// is an idempotent operation and it's okay if the reference didn't
|
|
|
|
// exist in the first place.
|
2015-09-29 20:40:17 +03:00
|
|
|
_, err := daemon.Repositories(ctx).Delete(repository, ref)
|
2015-08-15 10:30:25 +03:00
|
|
|
|
|
|
|
return utils.ImageReference(repository, ref), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeAllReferencesToImageID attempts to remove every reference to the given
|
|
|
|
// imgID from this daemon's store of repository tag/digest references. Returns
|
|
|
|
// on the first encountered error. Removed references are logged to this
|
|
|
|
// daemon's event service. An "Untagged" types.ImageDelete is added to the
|
|
|
|
// given list of records.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) removeAllReferencesToImageID(ctx context.Context, imgID string, records *[]types.ImageDelete) error {
|
2015-09-29 20:40:17 +03:00
|
|
|
imageRefs := daemon.Repositories(ctx).ByID()[imgID]
|
2015-08-15 10:30:25 +03:00
|
|
|
|
|
|
|
for _, imageRef := range imageRefs {
|
2015-09-11 01:01:18 +03:00
|
|
|
parsedRef, err := daemon.removeImageRef(ctx, imageRef)
|
2015-08-15 10:30:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
2015-08-15 10:30:25 +03:00
|
|
|
|
|
|
|
untaggedRecord := types.ImageDelete{Untagged: parsedRef}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
daemon.EventsService.Log(ctx, "untag", imgID, "")
|
2015-08-15 10:30:25 +03:00
|
|
|
*records = append(*records, untaggedRecord)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageDeleteConflict holds a soft or hard conflict and an associated error.
|
|
|
|
// Implements the error interface.
|
|
|
|
type imageDeleteConflict struct {
|
|
|
|
hard bool
|
|
|
|
imgID string
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idc *imageDeleteConflict) Error() string {
|
|
|
|
var forceMsg string
|
|
|
|
if idc.hard {
|
|
|
|
forceMsg = "cannot be forced"
|
2014-08-05 10:41:30 +04:00
|
|
|
} else {
|
2015-08-15 10:30:25 +03:00
|
|
|
forceMsg = "must be forced"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("conflict: unable to delete %s (%s) - %s", stringid.TruncateID(idc.imgID), forceMsg, idc.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// imageDeleteHelper attempts to delete the given image from this daemon. If
|
|
|
|
// the image has any hard delete conflicts (child images or running containers
|
|
|
|
// using the image) then it cannot be deleted. If the image has any soft delete
|
|
|
|
// conflicts (any tags/digests referencing the image or any stopped container
|
|
|
|
// using the image) then it can only be deleted if force is true. If the delete
|
|
|
|
// succeeds and prune is true, the parent images are also deleted if they do
|
|
|
|
// not have any soft or hard delete conflicts themselves. Any deleted images
|
|
|
|
// and untagged references are appended to the given records. If any error or
|
|
|
|
// conflict is encountered, it will be returned immediately without deleting
|
|
|
|
// the image. If quiet is true, any encountered conflicts will be ignored and
|
|
|
|
// the function will return nil immediately without deleting the image.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) imageDeleteHelper(ctx context.Context, img *image.Image, records *[]types.ImageDelete, force, prune, quiet bool) error {
|
2015-08-15 10:30:25 +03:00
|
|
|
// First, determine if this image has any conflicts. Ignore soft conflicts
|
|
|
|
// if force is true.
|
2015-09-11 01:01:18 +03:00
|
|
|
if conflict := daemon.checkImageDeleteConflict(ctx, img, force); conflict != nil {
|
|
|
|
if quiet && !daemon.imageIsDangling(ctx, img) {
|
2015-08-15 10:30:25 +03:00
|
|
|
// Ignore conflicts UNLESS the image is "dangling" in
|
|
|
|
// which case we want the user to know.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// There was a conflict and it's either a hard conflict OR we are not
|
|
|
|
// forcing deletion on soft conflicts.
|
|
|
|
return conflict
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all repository tag/digest references to this image.
|
2015-09-11 01:01:18 +03:00
|
|
|
if err := daemon.removeAllReferencesToImageID(ctx, img.ID, records); err != nil {
|
2015-08-15 10:30:25 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:40:17 +03:00
|
|
|
if err := daemon.Graph(ctx).Delete(img.ID); err != nil {
|
2015-08-15 10:30:25 +03:00
|
|
|
return err
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
daemon.EventsService.Log(ctx, "delete", img.ID, "")
|
2015-08-15 10:30:25 +03:00
|
|
|
*records = append(*records, types.ImageDelete{Deleted: img.ID})
|
|
|
|
|
|
|
|
if !prune || img.Parent == "" {
|
2014-08-05 10:41:30 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// We need to prune the parent image. This means delete it if there are
|
|
|
|
// no tags/digests referencing it and there are no containers using it (
|
|
|
|
// either running or stopped).
|
2015-09-29 20:40:17 +03:00
|
|
|
parentImg, err := daemon.Graph(ctx).Get(img.Parent)
|
2015-08-15 10:30:25 +03:00
|
|
|
if err != nil {
|
2015-09-18 20:48:16 +03:00
|
|
|
return derr.ErrorCodeImgNoParent.WithArgs(err)
|
2015-08-15 10:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not force prunings, but do so quietly (stopping on any encountered
|
|
|
|
// conflicts).
|
2015-09-11 01:01:18 +03:00
|
|
|
return daemon.imageDeleteHelper(ctx, parentImg, records, false, true, true)
|
2015-08-15 10:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkImageDeleteConflict determines whether there are any conflicts
|
|
|
|
// preventing deletion of the given image from this daemon. A hard conflict is
|
|
|
|
// any image which has the given image as a parent or any running container
|
|
|
|
// using the image. A soft conflict is any tags/digest referencing the given
|
|
|
|
// image or any stopped container using the image. If ignoreSoftConflicts is
|
|
|
|
// true, this function will not check for soft conflict conditions.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) checkImageDeleteConflict(ctx context.Context, img *image.Image, ignoreSoftConflicts bool) *imageDeleteConflict {
|
2015-08-15 10:30:25 +03:00
|
|
|
// Check for hard conflicts first.
|
2015-09-11 01:01:18 +03:00
|
|
|
if conflict := daemon.checkImageDeleteHardConflict(ctx, img); conflict != nil {
|
2015-08-15 10:30:25 +03:00
|
|
|
return conflict
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then check for soft conflicts.
|
|
|
|
if ignoreSoftConflicts {
|
|
|
|
// Don't bother checking for soft conflicts.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
return daemon.checkImageDeleteSoftConflict(ctx, img)
|
2015-08-15 10:30:25 +03:00
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) checkImageDeleteHardConflict(ctx context.Context, img *image.Image) *imageDeleteConflict {
|
2015-08-15 10:30:25 +03:00
|
|
|
// Check if the image ID is being used by a pull or build.
|
2015-09-29 20:40:17 +03:00
|
|
|
if daemon.Graph(ctx).IsHeld(img.ID) {
|
2015-08-15 10:30:25 +03:00
|
|
|
return &imageDeleteConflict{
|
|
|
|
hard: true,
|
|
|
|
imgID: img.ID,
|
|
|
|
message: "image is held by an ongoing pull or build",
|
2014-09-10 04:32:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// Check if the image has any descendent images.
|
2015-09-29 20:40:17 +03:00
|
|
|
if daemon.Graph(ctx).HasChildren(img) {
|
2015-08-15 10:30:25 +03:00
|
|
|
return &imageDeleteConflict{
|
|
|
|
hard: true,
|
|
|
|
imgID: img.ID,
|
|
|
|
message: "image has dependent child images",
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
// Check if any running container is using the image.
|
2015-09-29 20:40:17 +03:00
|
|
|
for _, container := range daemon.List(ctx) {
|
2015-08-15 10:30:25 +03:00
|
|
|
if !container.IsRunning() {
|
|
|
|
// Skip this until we check for soft conflicts later.
|
|
|
|
continue
|
|
|
|
}
|
2014-08-05 10:41:30 +04:00
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
if container.ImageID == img.ID {
|
|
|
|
return &imageDeleteConflict{
|
|
|
|
imgID: img.ID,
|
|
|
|
hard: true,
|
|
|
|
message: fmt.Sprintf("image is being used by running container %s", stringid.TruncateID(container.ID)),
|
|
|
|
}
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-15 10:30:25 +03:00
|
|
|
|
2014-08-05 10:41:30 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) checkImageDeleteSoftConflict(ctx context.Context, img *image.Image) *imageDeleteConflict {
|
2015-08-15 10:30:25 +03:00
|
|
|
// Check if any repository tags/digest reference this image.
|
2015-09-29 20:40:17 +03:00
|
|
|
if daemon.Repositories(ctx).HasReferences(img) {
|
2015-08-15 10:30:25 +03:00
|
|
|
return &imageDeleteConflict{
|
|
|
|
imgID: img.ID,
|
|
|
|
message: "image is referenced in one or more repositories",
|
|
|
|
}
|
2015-04-08 05:29:29 +03:00
|
|
|
}
|
2015-08-15 10:30:25 +03:00
|
|
|
|
|
|
|
// Check if any stopped containers reference this image.
|
2015-09-29 20:40:17 +03:00
|
|
|
for _, container := range daemon.List(ctx) {
|
2015-08-15 10:30:25 +03:00
|
|
|
if container.IsRunning() {
|
|
|
|
// Skip this as it was checked above in hard conflict conditions.
|
2015-05-28 04:54:54 +03:00
|
|
|
continue
|
|
|
|
}
|
2014-08-05 10:41:30 +04:00
|
|
|
|
2015-08-15 10:30:25 +03:00
|
|
|
if container.ImageID == img.ID {
|
|
|
|
return &imageDeleteConflict{
|
|
|
|
imgID: img.ID,
|
|
|
|
message: fmt.Sprintf("image is being used by stopped container %s", stringid.TruncateID(container.ID)),
|
2014-08-05 10:41:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-15 10:30:25 +03:00
|
|
|
|
2014-08-05 10:41:30 +04:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-15 10:30:25 +03:00
|
|
|
|
|
|
|
// imageIsDangling returns whether the given image is "dangling" which means
|
|
|
|
// that there are no repository references to the given image and it has no
|
|
|
|
// child images.
|
2015-09-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) imageIsDangling(ctx context.Context, img *image.Image) bool {
|
2015-09-29 20:40:17 +03:00
|
|
|
return !(daemon.Repositories(ctx).HasReferences(img) || daemon.Graph(ctx).HasChildren(img))
|
2015-08-15 10:30:25 +03:00
|
|
|
}
|