pull.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "golang.org/x/net/context"
  6. Cli "github.com/docker/docker/cli"
  7. "github.com/docker/docker/pkg/jsonmessage"
  8. flag "github.com/docker/docker/pkg/mflag"
  9. "github.com/docker/docker/reference"
  10. "github.com/docker/docker/registry"
  11. "github.com/docker/engine-api/types"
  12. )
  13. // CmdPull pulls an image or a repository from the registry.
  14. //
  15. // Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
  16. func (cli *DockerCli) CmdPull(args ...string) error {
  17. cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, Cli.DockerCommands["pull"].Description, true)
  18. allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
  19. addTrustedFlags(cmd, true)
  20. cmd.Require(flag.Exact, 1)
  21. cmd.ParseFlags(args, true)
  22. remote := cmd.Arg(0)
  23. distributionRef, err := reference.ParseNamed(remote)
  24. if err != nil {
  25. return err
  26. }
  27. if *allTags && !reference.IsNameOnly(distributionRef) {
  28. return errors.New("tag can't be used with --all-tags/-a")
  29. }
  30. if !*allTags && reference.IsNameOnly(distributionRef) {
  31. distributionRef = reference.WithDefaultTag(distributionRef)
  32. fmt.Fprintf(cli.out, "Using default tag: %s\n", reference.DefaultTag)
  33. }
  34. var tag string
  35. switch x := distributionRef.(type) {
  36. case reference.Canonical:
  37. tag = x.Digest().String()
  38. case reference.NamedTagged:
  39. tag = x.Tag()
  40. }
  41. registryRef := registry.ParseReference(tag)
  42. // Resolve the Repository name from fqn to RepositoryInfo
  43. repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
  44. if err != nil {
  45. return err
  46. }
  47. ctx := context.Background()
  48. authConfig := cli.resolveAuthConfig(ctx, repoInfo.Index)
  49. requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
  50. if isTrusted() && !registryRef.HasDigest() {
  51. // Check if tag is digest
  52. return cli.trustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege)
  53. }
  54. return cli.imagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, *allTags)
  55. }
  56. func (cli *DockerCli) imagePullPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
  57. encodedAuth, err := encodeAuthToBase64(authConfig)
  58. if err != nil {
  59. return err
  60. }
  61. options := types.ImagePullOptions{
  62. RegistryAuth: encodedAuth,
  63. PrivilegeFunc: requestPrivilege,
  64. All: all,
  65. }
  66. responseBody, err := cli.client.ImagePull(ctx, ref, options)
  67. if err != nil {
  68. return err
  69. }
  70. defer responseBody.Close()
  71. return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
  72. }