push.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client
  2. import (
  3. "fmt"
  4. "net/url"
  5. Cli "github.com/docker/docker/cli"
  6. flag "github.com/docker/docker/pkg/mflag"
  7. "github.com/docker/docker/pkg/parsers"
  8. "github.com/docker/docker/registry"
  9. )
  10. // CmdPush pushes an image or repository to the registry.
  11. //
  12. // Usage: docker push NAME[:TAG]
  13. func (cli *DockerCli) CmdPush(args ...string) error {
  14. cmd := Cli.Subcmd("push", []string{"NAME[:TAG]"}, "Push an image or a repository to a registry", true)
  15. addTrustedFlags(cmd, false)
  16. cmd.Require(flag.Exact, 1)
  17. cmd.ParseFlags(args, true)
  18. remote, tag := parsers.ParseRepositoryTag(cmd.Arg(0))
  19. // Resolve the Repository name from fqn to RepositoryInfo
  20. repoInfo, err := registry.ParseRepositoryInfo(remote)
  21. if err != nil {
  22. return err
  23. }
  24. // Resolve the Auth config relevant for this server
  25. authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index)
  26. // If we're not using a custom registry, we know the restrictions
  27. // applied to repository names and can warn the user in advance.
  28. // Custom repositories can have different rules, and we must also
  29. // allow pushing by image ID.
  30. if repoInfo.Official {
  31. username := authConfig.Username
  32. if username == "" {
  33. username = "<user>"
  34. }
  35. return fmt.Errorf("You cannot push a \"root\" repository. Please rename your repository to <user>/<repo> (ex: %s/%s)", username, repoInfo.LocalName)
  36. }
  37. if isTrusted() {
  38. return cli.trustedPush(repoInfo, tag, authConfig)
  39. }
  40. v := url.Values{}
  41. v.Set("tag", tag)
  42. _, _, err = cli.clientRequestAttemptLogin("POST", "/images/"+remote+"/push?"+v.Encode(), nil, cli.out, repoInfo.Index, "push")
  43. return err
  44. }