github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/daemon/image_delete.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/docker/distribution/reference"
     9  	"github.com/docker/docker/api/errors"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/container"
    12  	"github.com/docker/docker/image"
    13  	"github.com/docker/docker/pkg/stringid"
    14  )
    15  
    16  type conflictType int
    17  
    18  const (
    19  	conflictDependentChild conflictType = (1 << iota)
    20  	conflictRunningContainer
    21  	conflictActiveReference
    22  	conflictStoppedContainer
    23  	conflictHard = conflictDependentChild | conflictRunningContainer
    24  	conflictSoft = conflictActiveReference | conflictStoppedContainer
    25  )
    26  
    27  // ImageDelete deletes the image referenced by the given imageRef from this
    28  // daemon. The given imageRef can be an image ID, ID prefix, or a repository
    29  // reference (with an optional tag or digest, defaulting to the tag name
    30  // "latest"). There is differing behavior depending on whether the given
    31  // imageRef is a repository reference or not.
    32  //
    33  // If the given imageRef is a repository reference then that repository
    34  // reference will be removed. However, if there exists any containers which
    35  // were created using the same image reference then the repository reference
    36  // cannot be removed unless either there are other repository references to the
    37  // same image or force is true. Following removal of the repository reference,
    38  // the referenced image itself will attempt to be deleted as described below
    39  // but quietly, meaning any image delete conflicts will cause the image to not
    40  // be deleted and the conflict will not be reported.
    41  //
    42  // There may be conflicts preventing deletion of an image and these conflicts
    43  // are divided into two categories grouped by their severity:
    44  //
    45  // Hard Conflict:
    46  // 	- a pull or build using the image.
    47  // 	- any descendant image.
    48  // 	- any running container using the image.
    49  //
    50  // Soft Conflict:
    51  // 	- any stopped container using the image.
    52  // 	- any repository tag or digest references to the image.
    53  //
    54  // The image cannot be removed if there are any hard conflicts and can be
    55  // removed if there are soft conflicts only if force is true.
    56  //
    57  // If prune is true, ancestor images will each attempt to be deleted quietly,
    58  // meaning any delete conflicts will cause the image to not be deleted and the
    59  // conflict will not be reported.
    60  //
    61  // FIXME: remove ImageDelete's dependency on Daemon, then move to the graph
    62  // package. This would require that we no longer need the daemon to determine
    63  // whether images are being used by a stopped or running container.
    64  func (daemon *Daemon) ImageDelete(imageRef string, force, prune bool) ([]types.ImageDeleteResponseItem, error) {
    65  	start := time.Now()
    66  	records := []types.ImageDeleteResponseItem{}
    67  
    68  	imgID, err := daemon.GetImageID(imageRef)
    69  	if err != nil {
    70  		return nil, daemon.imageNotExistToErrcode(err)
    71  	}
    72  
    73  	repoRefs := daemon.referenceStore.References(imgID.Digest())
    74  
    75  	var removedRepositoryRef bool
    76  	if !isImageIDPrefix(imgID.String(), imageRef) {
    77  		// A repository reference was given and should be removed
    78  		// first. We can only remove this reference if either force is
    79  		// true, there are multiple repository references to this
    80  		// image, or there are no containers using the given reference.
    81  		if !force && isSingleReference(repoRefs) {
    82  			if container := daemon.getContainerUsingImage(imgID); container != nil {
    83  				// If we removed the repository reference then
    84  				// this image would remain "dangling" and since
    85  				// we really want to avoid that the client must
    86  				// explicitly force its removal.
    87  				err := fmt.Errorf("conflict: unable to remove repository reference %q (must force) - container %s is using its referenced image %s", imageRef, stringid.TruncateID(container.ID), stringid.TruncateID(imgID.String()))
    88  				return nil, errors.NewRequestConflictError(err)
    89  			}
    90  		}
    91  
    92  		parsedRef, err := reference.ParseNormalizedNamed(imageRef)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  
    97  		parsedRef, err = daemon.removeImageRef(parsedRef)
    98  		if err != nil {
    99  			return nil, err
   100  		}
   101  
   102  		untaggedRecord := types.ImageDeleteResponseItem{Untagged: reference.FamiliarString(parsedRef)}
   103  
   104  		daemon.LogImageEvent(imgID.String(), imgID.String(), "untag")
   105  		records = append(records, untaggedRecord)
   106  
   107  		repoRefs = daemon.referenceStore.References(imgID.Digest())
   108  
   109  		// If a tag reference was removed and the only remaining
   110  		// references to the same repository are digest references,
   111  		// then clean up those digest references.
   112  		if _, isCanonical := parsedRef.(reference.Canonical); !isCanonical {
   113  			foundRepoTagRef := false
   114  			for _, repoRef := range repoRefs {
   115  				if _, repoRefIsCanonical := repoRef.(reference.Canonical); !repoRefIsCanonical && parsedRef.Name() == repoRef.Name() {
   116  					foundRepoTagRef = true
   117  					break
   118  				}
   119  			}
   120  			if !foundRepoTagRef {
   121  				// Remove canonical references from same repository
   122  				remainingRefs := []reference.Named{}
   123  				for _, repoRef := range repoRefs {
   124  					if _, repoRefIsCanonical := repoRef.(reference.Canonical); repoRefIsCanonical && parsedRef.Name() == repoRef.Name() {
   125  						if _, err := daemon.removeImageRef(repoRef); err != nil {
   126  							return records, err
   127  						}
   128  
   129  						untaggedRecord := types.ImageDeleteResponseItem{Untagged: reference.FamiliarString(repoRef)}
   130  						records = append(records, untaggedRecord)
   131  					} else {
   132  						remainingRefs = append(remainingRefs, repoRef)
   133  
   134  					}
   135  				}
   136  				repoRefs = remainingRefs
   137  			}
   138  		}
   139  
   140  		// If it has remaining references then the untag finished the remove
   141  		if len(repoRefs) > 0 {
   142  			return records, nil
   143  		}
   144  
   145  		removedRepositoryRef = true
   146  	} else {
   147  		// If an ID reference was given AND there is at most one tag
   148  		// reference to the image AND all references are within one
   149  		// repository, then remove all references.
   150  		if isSingleReference(repoRefs) {
   151  			c := conflictHard
   152  			if !force {
   153  				c |= conflictSoft &^ conflictActiveReference
   154  			}
   155  			if conflict := daemon.checkImageDeleteConflict(imgID, c); conflict != nil {
   156  				return nil, conflict
   157  			}
   158  
   159  			for _, repoRef := range repoRefs {
   160  				parsedRef, err := daemon.removeImageRef(repoRef)
   161  				if err != nil {
   162  					return nil, err
   163  				}
   164  
   165  				untaggedRecord := types.ImageDeleteResponseItem{Untagged: reference.FamiliarString(parsedRef)}
   166  
   167  				daemon.LogImageEvent(imgID.String(), imgID.String(), "untag")
   168  				records = append(records, untaggedRecord)
   169  			}
   170  		}
   171  	}
   172  
   173  	if err := daemon.imageDeleteHelper(imgID, &records, force, prune, removedRepositoryRef); err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	imageActions.WithValues("delete").UpdateSince(start)
   178  
   179  	return records, nil
   180  }
   181  
   182  // isSingleReference returns true when all references are from one repository
   183  // and there is at most one tag. Returns false for empty input.
   184  func isSingleReference(repoRefs []reference.Named) bool {
   185  	if len(repoRefs) <= 1 {
   186  		return len(repoRefs) == 1
   187  	}
   188  	var singleRef reference.Named
   189  	canonicalRefs := map[string]struct{}{}
   190  	for _, repoRef := range repoRefs {
   191  		if _, isCanonical := repoRef.(reference.Canonical); isCanonical {
   192  			canonicalRefs[repoRef.Name()] = struct{}{}
   193  		} else if singleRef == nil {
   194  			singleRef = repoRef
   195  		} else {
   196  			return false
   197  		}
   198  	}
   199  	if singleRef == nil {
   200  		// Just use first canonical ref
   201  		singleRef = repoRefs[0]
   202  	}
   203  	_, ok := canonicalRefs[singleRef.Name()]
   204  	return len(canonicalRefs) == 1 && ok
   205  }
   206  
   207  // isImageIDPrefix returns whether the given possiblePrefix is a prefix of the
   208  // given imageID.
   209  func isImageIDPrefix(imageID, possiblePrefix string) bool {
   210  	if strings.HasPrefix(imageID, possiblePrefix) {
   211  		return true
   212  	}
   213  
   214  	if i := strings.IndexRune(imageID, ':'); i >= 0 {
   215  		return strings.HasPrefix(imageID[i+1:], possiblePrefix)
   216  	}
   217  
   218  	return false
   219  }
   220  
   221  // getContainerUsingImage returns a container that was created using the given
   222  // imageID. Returns nil if there is no such container.
   223  func (daemon *Daemon) getContainerUsingImage(imageID image.ID) *container.Container {
   224  	return daemon.containers.First(func(c *container.Container) bool {
   225  		return c.ImageID == imageID
   226  	})
   227  }
   228  
   229  // removeImageRef attempts to parse and remove the given image reference from
   230  // this daemon's store of repository tag/digest references. The given
   231  // repositoryRef must not be an image ID but a repository name followed by an
   232  // optional tag or digest reference. If tag or digest is omitted, the default
   233  // tag is used. Returns the resolved image reference and an error.
   234  func (daemon *Daemon) removeImageRef(ref reference.Named) (reference.Named, error) {
   235  	ref = reference.TagNameOnly(ref)
   236  
   237  	// Ignore the boolean value returned, as far as we're concerned, this
   238  	// is an idempotent operation and it's okay if the reference didn't
   239  	// exist in the first place.
   240  	_, err := daemon.referenceStore.Delete(ref)
   241  
   242  	return ref, err
   243  }
   244  
   245  // removeAllReferencesToImageID attempts to remove every reference to the given
   246  // imgID from this daemon's store of repository tag/digest references. Returns
   247  // on the first encountered error. Removed references are logged to this
   248  // daemon's event service. An "Untagged" types.ImageDeleteResponseItem is added to the
   249  // given list of records.
   250  func (daemon *Daemon) removeAllReferencesToImageID(imgID image.ID, records *[]types.ImageDeleteResponseItem) error {
   251  	imageRefs := daemon.referenceStore.References(imgID.Digest())
   252  
   253  	for _, imageRef := range imageRefs {
   254  		parsedRef, err := daemon.removeImageRef(imageRef)
   255  		if err != nil {
   256  			return err
   257  		}
   258  
   259  		untaggedRecord := types.ImageDeleteResponseItem{Untagged: reference.FamiliarString(parsedRef)}
   260  
   261  		daemon.LogImageEvent(imgID.String(), imgID.String(), "untag")
   262  		*records = append(*records, untaggedRecord)
   263  	}
   264  
   265  	return nil
   266  }
   267  
   268  // ImageDeleteConflict holds a soft or hard conflict and an associated error.
   269  // Implements the error interface.
   270  type imageDeleteConflict struct {
   271  	hard    bool
   272  	used    bool
   273  	imgID   image.ID
   274  	message string
   275  }
   276  
   277  func (idc *imageDeleteConflict) Error() string {
   278  	var forceMsg string
   279  	if idc.hard {
   280  		forceMsg = "cannot be forced"
   281  	} else {
   282  		forceMsg = "must be forced"
   283  	}
   284  
   285  	return fmt.Sprintf("conflict: unable to delete %s (%s) - %s", stringid.TruncateID(idc.imgID.String()), forceMsg, idc.message)
   286  }
   287  
   288  // imageDeleteHelper attempts to delete the given image from this daemon. If
   289  // the image has any hard delete conflicts (child images or running containers
   290  // using the image) then it cannot be deleted. If the image has any soft delete
   291  // conflicts (any tags/digests referencing the image or any stopped container
   292  // using the image) then it can only be deleted if force is true. If the delete
   293  // succeeds and prune is true, the parent images are also deleted if they do
   294  // not have any soft or hard delete conflicts themselves. Any deleted images
   295  // and untagged references are appended to the given records. If any error or
   296  // conflict is encountered, it will be returned immediately without deleting
   297  // the image. If quiet is true, any encountered conflicts will be ignored and
   298  // the function will return nil immediately without deleting the image.
   299  func (daemon *Daemon) imageDeleteHelper(imgID image.ID, records *[]types.ImageDeleteResponseItem, force, prune, quiet bool) error {
   300  	// First, determine if this image has any conflicts. Ignore soft conflicts
   301  	// if force is true.
   302  	c := conflictHard
   303  	if !force {
   304  		c |= conflictSoft
   305  	}
   306  	if conflict := daemon.checkImageDeleteConflict(imgID, c); conflict != nil {
   307  		if quiet && (!daemon.imageIsDangling(imgID) || conflict.used) {
   308  			// Ignore conflicts UNLESS the image is "dangling" or not being used in
   309  			// which case we want the user to know.
   310  			return nil
   311  		}
   312  
   313  		// There was a conflict and it's either a hard conflict OR we are not
   314  		// forcing deletion on soft conflicts.
   315  		return conflict
   316  	}
   317  
   318  	parent, err := daemon.imageStore.GetParent(imgID)
   319  	if err != nil {
   320  		// There may be no parent
   321  		parent = ""
   322  	}
   323  
   324  	// Delete all repository tag/digest references to this image.
   325  	if err := daemon.removeAllReferencesToImageID(imgID, records); err != nil {
   326  		return err
   327  	}
   328  
   329  	removedLayers, err := daemon.imageStore.Delete(imgID)
   330  	if err != nil {
   331  		return err
   332  	}
   333  
   334  	daemon.LogImageEvent(imgID.String(), imgID.String(), "delete")
   335  	*records = append(*records, types.ImageDeleteResponseItem{Deleted: imgID.String()})
   336  	for _, removedLayer := range removedLayers {
   337  		*records = append(*records, types.ImageDeleteResponseItem{Deleted: removedLayer.ChainID.String()})
   338  	}
   339  
   340  	if !prune || parent == "" {
   341  		return nil
   342  	}
   343  
   344  	// We need to prune the parent image. This means delete it if there are
   345  	// no tags/digests referencing it and there are no containers using it (
   346  	// either running or stopped).
   347  	// Do not force prunings, but do so quietly (stopping on any encountered
   348  	// conflicts).
   349  	return daemon.imageDeleteHelper(parent, records, false, true, true)
   350  }
   351  
   352  // checkImageDeleteConflict determines whether there are any conflicts
   353  // preventing deletion of the given image from this daemon. A hard conflict is
   354  // any image which has the given image as a parent or any running container
   355  // using the image. A soft conflict is any tags/digest referencing the given
   356  // image or any stopped container using the image. If ignoreSoftConflicts is
   357  // true, this function will not check for soft conflict conditions.
   358  func (daemon *Daemon) checkImageDeleteConflict(imgID image.ID, mask conflictType) *imageDeleteConflict {
   359  	// Check if the image has any descendant images.
   360  	if mask&conflictDependentChild != 0 && len(daemon.imageStore.Children(imgID)) > 0 {
   361  		return &imageDeleteConflict{
   362  			hard:    true,
   363  			imgID:   imgID,
   364  			message: "image has dependent child images",
   365  		}
   366  	}
   367  
   368  	if mask&conflictRunningContainer != 0 {
   369  		// Check if any running container is using the image.
   370  		running := func(c *container.Container) bool {
   371  			return c.IsRunning() && c.ImageID == imgID
   372  		}
   373  		if container := daemon.containers.First(running); container != nil {
   374  			return &imageDeleteConflict{
   375  				imgID:   imgID,
   376  				hard:    true,
   377  				used:    true,
   378  				message: fmt.Sprintf("image is being used by running container %s", stringid.TruncateID(container.ID)),
   379  			}
   380  		}
   381  	}
   382  
   383  	// Check if any repository tags/digest reference this image.
   384  	if mask&conflictActiveReference != 0 && len(daemon.referenceStore.References(imgID.Digest())) > 0 {
   385  		return &imageDeleteConflict{
   386  			imgID:   imgID,
   387  			message: "image is referenced in multiple repositories",
   388  		}
   389  	}
   390  
   391  	if mask&conflictStoppedContainer != 0 {
   392  		// Check if any stopped containers reference this image.
   393  		stopped := func(c *container.Container) bool {
   394  			return !c.IsRunning() && c.ImageID == imgID
   395  		}
   396  		if container := daemon.containers.First(stopped); container != nil {
   397  			return &imageDeleteConflict{
   398  				imgID:   imgID,
   399  				used:    true,
   400  				message: fmt.Sprintf("image is being used by stopped container %s", stringid.TruncateID(container.ID)),
   401  			}
   402  		}
   403  	}
   404  
   405  	return nil
   406  }
   407  
   408  // imageIsDangling returns whether the given image is "dangling" which means
   409  // that there are no repository references to the given image and it has no
   410  // child images.
   411  func (daemon *Daemon) imageIsDangling(imgID image.ID) bool {
   412  	return !(len(daemon.referenceStore.References(imgID.Digest())) > 0 || len(daemon.imageStore.Children(imgID)) > 0)
   413  }