push.go 1.4 KB

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