pull.go 2.5 KB

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