2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/url"
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2017-01-03 10:02:58 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageTag tags an image in the docker host
|
2017-01-03 10:02:58 -05:00
|
|
|
func (cli *Client) ImageTag(ctx context.Context, source, target string) error {
|
2017-04-11 16:37:04 -04:00
|
|
|
if _, err := reference.ParseAnyReference(source); err != nil {
|
2017-01-03 10:02:58 -05:00
|
|
|
return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", source)
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(target)
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
2017-01-03 10:02:58 -05:00
|
|
|
return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", target)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
2016-09-06 14:46:37 -04:00
|
|
|
return errors.New("refusing to create a tag with a digest reference")
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
ref = reference.TagNameOnly(ref)
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
query := url.Values{}
|
2017-01-25 19:54:18 -05:00
|
|
|
query.Set("repo", reference.FamiliarName(ref))
|
|
|
|
if tagged, ok := ref.(reference.Tagged); ok {
|
|
|
|
query.Set("tag", tagged.Tag())
|
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2017-01-03 10:02:58 -05:00
|
|
|
resp, err := cli.post(ctx, "/images/"+source+"/tag", query, nil, nil)
|
2016-09-06 14:46:37 -04:00
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return err
|
|
|
|
}
|