push.go 1.5 KB

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