pull.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. Cli "github.com/docker/docker/cli"
  6. "github.com/docker/docker/pkg/jsonmessage"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. "github.com/docker/docker/reference"
  9. "github.com/docker/docker/registry"
  10. "github.com/docker/engine-api/client"
  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. ref := 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. authConfig := cli.resolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
  48. requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
  49. if isTrusted() && !ref.HasDigest() {
  50. // Check if tag is digest
  51. return cli.trustedPull(repoInfo, ref, authConfig, requestPrivilege)
  52. }
  53. return cli.imagePullPrivileged(authConfig, distributionRef.String(), "", requestPrivilege)
  54. }
  55. func (cli *DockerCli) imagePullPrivileged(authConfig types.AuthConfig, imageID, tag string, requestPrivilege client.RequestPrivilegeFunc) error {
  56. encodedAuth, err := encodeAuthToBase64(authConfig)
  57. if err != nil {
  58. return err
  59. }
  60. options := types.ImagePullOptions{
  61. ImageID: imageID,
  62. Tag: tag,
  63. RegistryAuth: encodedAuth,
  64. }
  65. responseBody, err := cli.client.ImagePull(options, requestPrivilege)
  66. if err != nil {
  67. return err
  68. }
  69. defer responseBody.Close()
  70. return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
  71. }