2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2023-07-10 14:44:59 +00:00
|
|
|
"net/http"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/url"
|
|
|
|
|
2023-08-30 16:31:46 +00:00
|
|
|
"github.com/distribution/reference"
|
2016-09-06 18:46:37 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2021-08-26 19:08:38 +00:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2019-02-09 18:19:22 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2017-01-26 00:54:18 +00:00
|
|
|
func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(image)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
2016-09-06 18:46:37 +00:00
|
|
|
return nil, errors.New("cannot push a digest reference")
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
name := reference.FamiliarName(ref)
|
2016-09-06 18:46:37 +00:00
|
|
|
query := url.Values{}
|
client.ImagePush(): default to ":latest" instead of "all tags"
The `docker push` command up until docker v0.9.1 always pushed all tags of a given
image, so `docker push foo/bar` would push (e.g.) all of `foo/bar:latest`, `foo:/bar:v1`,
and `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see issue 3411 and PR 4948 (commit e648a186d68dcb3ee0d6123b041c5aa66438cc89).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image".
`docker pull` had a similar behavior, but PR 7759 (9c08364a412a51c80e8d17ae14f92549dc543e68)
changed the behavior to default to the `:latest` tag, and added a `--all-tags` flag
to the CLI to optionally pull all images.
This patch implements the API client changes to make `docker push` match the behavior
of `docker pull`, and default to pull a single image, unless the `all` option is passed.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-10 11:24:14 +00:00
|
|
|
if !options.All {
|
|
|
|
ref = reference.TagNameOnly(ref)
|
|
|
|
if tagged, ok := ref.(reference.Tagged); ok {
|
|
|
|
query.Set("tag", tagged.Tag())
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
|
2019-02-09 18:19:22 +00:00
|
|
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
2016-09-06 18:46:37 +00:00
|
|
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
|
|
|
if privilegeErr != nil {
|
|
|
|
return nil, privilegeErr
|
|
|
|
}
|
2017-01-26 00:54:18 +00:00
|
|
|
resp, err = cli.tryImagePush(ctx, name, query, newAuthHeader)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
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) {
|
2023-07-10 14:44:59 +00:00
|
|
|
return cli.post(ctx, "/images/"+imageID+"/push", query, nil, http.Header{
|
|
|
|
registry.AuthHeader: {registryAuth},
|
|
|
|
})
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|