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