4f0d95fa |
package client // import "github.com/docker/docker/client" |
7c36a1af |
import ( |
7d62e40f |
"context" |
7c36a1af |
"errors"
"io"
"net/url"
|
3a127939 |
"github.com/docker/distribution/reference" |
7c36a1af |
"github.com/docker/docker/api/types" |
0cafc84f |
"github.com/docker/docker/errdefs" |
7c36a1af |
)
// ImagePush requests the docker host to push an image to a remote registry.
// It executes the privileged function if the operation is unauthorized
// and it tries one more time.
// It's up to the caller to handle the io.ReadCloser and close it properly. |
3a127939 |
func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
ref, err := reference.ParseNormalizedNamed(image) |
7c36a1af |
if err != nil {
return nil, err
}
|
3a127939 |
if _, isCanonical := ref.(reference.Canonical); isCanonical { |
7c36a1af |
return nil, errors.New("cannot push a digest reference")
}
|
3a127939 |
tag := ""
name := reference.FamiliarName(ref)
if nameTaggedRef, isNamedTagged := ref.(reference.NamedTagged); isNamedTagged { |
7c36a1af |
tag = nameTaggedRef.Tag()
}
query := url.Values{}
query.Set("tag", tag)
|
3a127939 |
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth) |
0cafc84f |
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil { |
7c36a1af |
newAuthHeader, privilegeErr := options.PrivilegeFunc()
if privilegeErr != nil {
return nil, privilegeErr
} |
3a127939 |
resp, err = cli.tryImagePush(ctx, name, query, newAuthHeader) |
7c36a1af |
}
if err != nil {
return nil, err
}
return resp.body, nil
}
func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) {
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers)
} |