7 "github.com/docker/cli/cli"
8 "github.com/docker/cli/cli/command"
9 "github.com/docker/distribution/reference"
10 "github.com/docker/docker/registry"
11 "github.com/pkg/errors"
12 "github.com/spf13/cobra"
13 "golang.org/x/net/context"
16 type pullOptions struct {
21 // NewPullCommand creates a new `docker pull` command
22 func NewPullCommand(dockerCli command.Cli) *cobra.Command {
25 cmd := &cobra.Command{
26 Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]",
27 Short: "Pull an image or a repository from a registry",
28 Args: cli.ExactArgs(1),
29 RunE: func(cmd *cobra.Command, args []string) error {
31 return runPull(dockerCli, opts)
37 flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
38 command.AddTrustVerificationFlags(flags)
43 func runPull(dockerCli command.Cli, opts pullOptions) error {
44 distributionRef, err := reference.ParseNormalizedNamed(opts.remote)
48 if opts.all && !reference.IsNameOnly(distributionRef) {
49 return errors.New("tag can't be used with --all-tags/-a")
52 if !opts.all && reference.IsNameOnly(distributionRef) {
53 distributionRef = reference.TagNameOnly(distributionRef)
54 if tagged, ok := distributionRef.(reference.Tagged); ok {
55 fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", tagged.Tag())
59 // Resolve the Repository name from fqn to RepositoryInfo
60 repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
65 ctx := context.Background()
67 authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
68 requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "pull")
70 // Check if reference has a digest
71 _, isCanonical := distributionRef.(reference.Canonical)
72 if command.IsTrusted() && !isCanonical {
73 err = trustedPull(ctx, dockerCli, repoInfo, distributionRef, authConfig, requestPrivilege)
75 err = imagePullPrivileged(ctx, dockerCli, authConfig, reference.FamiliarString(distributionRef), requestPrivilege, opts.all)
78 if strings.Contains(err.Error(), "when fetching 'plugin'") {
79 return errors.New(err.Error() + " - Use `docker plugin install`")