github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/moby/daemon/images/image.go (about) 1 package images // import "github.com/docker/docker/daemon/images" 2 3 import ( 4 "fmt" 5 6 "github.com/containerd/containerd/platforms" 7 "github.com/pkg/errors" 8 9 "github.com/docker/distribution/reference" 10 "github.com/docker/docker/errdefs" 11 "github.com/docker/docker/image" 12 specs "github.com/opencontainers/image-spec/specs-go/v1" 13 ) 14 15 // ErrImageDoesNotExist is error returned when no image can be found for a reference. 16 type ErrImageDoesNotExist struct { 17 ref reference.Reference 18 } 19 20 func (e ErrImageDoesNotExist) Error() string { 21 ref := e.ref 22 if named, ok := ref.(reference.Named); ok { 23 ref = reference.TagNameOnly(named) 24 } 25 return fmt.Sprintf("No such image: %s", reference.FamiliarString(ref)) 26 } 27 28 // NotFound implements the NotFound interface 29 func (e ErrImageDoesNotExist) NotFound() {} 30 31 // GetImage returns an image corresponding to the image referred to by refOrID. 32 func (i *ImageService) GetImage(refOrID string, platform *specs.Platform) (retImg *image.Image, retErr error) { 33 defer func() { 34 if retErr != nil || retImg == nil || platform == nil { 35 return 36 } 37 38 imgPlat := specs.Platform{ 39 OS: retImg.OS, 40 Architecture: retImg.Architecture, 41 Variant: retImg.Variant, 42 } 43 p := *platform 44 // Note that `platforms.Only` will fuzzy match this for us 45 // For example: an armv6 image will run just fine an an armv7 CPU, without emulation or anything. 46 if !platforms.Only(p).Match(imgPlat) { 47 // This allows us to tell clients that we don't have the image they asked for 48 // Where this gets hairy is the image store does not currently support multi-arch images, e.g.: 49 // An image `foo` may have a multi-arch manifest, but the image store only fetches the image for a specific platform 50 // The image store does not store the manifest list and image tags are assigned to architecture specific images. 51 // So we can have a `foo` image that is amd64 but the user requested armv7. If the user looks at the list of images. 52 // This may be confusing. 53 // The alternative to this is to return a errdefs.Conflict error with a helpful message, but clients will not be 54 // able to automatically tell what causes the conflict. 55 retErr = errdefs.NotFound(errors.Errorf("image with reference %s was found but does not match the specified platform: wanted %s, actual: %s", refOrID, platforms.Format(p), platforms.Format(imgPlat))) 56 return 57 } 58 }() 59 ref, err := reference.ParseAnyReference(refOrID) 60 if err != nil { 61 return nil, errdefs.InvalidParameter(err) 62 } 63 namedRef, ok := ref.(reference.Named) 64 if !ok { 65 digested, ok := ref.(reference.Digested) 66 if !ok { 67 return nil, ErrImageDoesNotExist{ref} 68 } 69 id := image.IDFromDigest(digested.Digest()) 70 if img, err := i.imageStore.Get(id); err == nil { 71 return img, nil 72 } 73 return nil, ErrImageDoesNotExist{ref} 74 } 75 76 if digest, err := i.referenceStore.Get(namedRef); err == nil { 77 // Search the image stores to get the operating system, defaulting to host OS. 78 id := image.IDFromDigest(digest) 79 if img, err := i.imageStore.Get(id); err == nil { 80 return img, nil 81 } 82 } 83 84 // Search based on ID 85 if id, err := i.imageStore.Search(refOrID); err == nil { 86 img, err := i.imageStore.Get(id) 87 if err != nil { 88 return nil, ErrImageDoesNotExist{ref} 89 } 90 return img, nil 91 } 92 93 return nil, ErrImageDoesNotExist{ref} 94 }