github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/images/image_tag.go (about)

     1  package images // import "github.com/docker/docker/daemon/images"
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/distribution/reference"
     7  	imagetypes "github.com/docker/docker/api/types/image"
     8  	"github.com/docker/docker/image"
     9  )
    10  
    11  // TagImage creates the tag specified by newTag, pointing to the image named
    12  // imageName (alternatively, imageName can also be an image ID).
    13  func (i *ImageService) TagImage(imageName, repository, tag string) (string, error) {
    14  	ctx := context.TODO()
    15  	img, err := i.GetImage(ctx, imageName, imagetypes.GetImageOpts{})
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  
    20  	newTag, err := reference.ParseNormalizedNamed(repository)
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  	if tag != "" {
    25  		if newTag, err = reference.WithTag(reference.TrimNamed(newTag), tag); err != nil {
    26  			return "", err
    27  		}
    28  	}
    29  
    30  	err = i.TagImageWithReference(img.ID(), newTag)
    31  	return reference.FamiliarString(newTag), err
    32  }
    33  
    34  // TagImageWithReference adds the given reference to the image ID provided.
    35  func (i *ImageService) TagImageWithReference(imageID image.ID, newTag reference.Named) error {
    36  	if err := i.referenceStore.AddTag(newTag, imageID.Digest(), true); err != nil {
    37  		return err
    38  	}
    39  
    40  	if err := i.imageStore.SetLastUpdated(imageID); err != nil {
    41  		return err
    42  	}
    43  	i.LogImageEvent(imageID.String(), reference.FamiliarString(newTag), "tag")
    44  	return nil
    45  }