newCreateCommand creates a new cobra.Command for `docker create`
(dockerCLI command.Cli)
| 46 | |
| 47 | // newCreateCommand creates a new cobra.Command for `docker create` |
| 48 | func newCreateCommand(dockerCLI command.Cli) *cobra.Command { |
| 49 | var options createOptions |
| 50 | var copts *containerOptions |
| 51 | |
| 52 | cmd := &cobra.Command{ |
| 53 | Use: "create [OPTIONS] IMAGE [COMMAND] [ARG...]", |
| 54 | Short: "Create a new container", |
| 55 | Args: cli.RequiresMinArgs(1), |
| 56 | RunE: func(cmd *cobra.Command, args []string) error { |
| 57 | copts.Image = args[0] |
| 58 | if len(args) > 1 { |
| 59 | copts.Args = args[1:] |
| 60 | } |
| 61 | return runCreate(cmd.Context(), dockerCLI, cmd.Flags(), &options, copts) |
| 62 | }, |
| 63 | Annotations: map[string]string{ |
| 64 | "aliases": "docker container create, docker create", |
| 65 | }, |
| 66 | ValidArgsFunction: completion.ImageNames(dockerCLI, -1), |
| 67 | DisableFlagsInUseLine: true, |
| 68 | } |
| 69 | |
| 70 | flags := cmd.Flags() |
| 71 | flags.SetInterspersed(false) |
| 72 | |
| 73 | flags.StringVar(&options.name, "name", "", "Assign a name to the container") |
| 74 | flags.StringVar(&options.pull, "pull", PullImageMissing, `Pull image before creating ("`+PullImageAlways+`", "|`+PullImageMissing+`", "`+PullImageNever+`")`) |
| 75 | flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the pull output") |
| 76 | flags.BoolVarP(&options.useAPISocket, "use-api-socket", "", false, "Bind mount Docker API socket and required auth") |
| 77 | _ = flags.SetAnnotation("use-api-socket", "experimentalCLI", nil) // Mark flag as experimental for now. |
| 78 | |
| 79 | // Add an explicit help that doesn't have a `-h` to prevent the conflict |
| 80 | // with hostname |
| 81 | flags.Bool("help", false, "Print usage") |
| 82 | |
| 83 | // TODO(thaJeztah): consider adding platform as "image create option" on containerOptions |
| 84 | flags.StringVar(&options.platform, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable") |
| 85 | _ = flags.SetAnnotation("platform", "version", []string{"1.32"}) |
| 86 | _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms()) |
| 87 | |
| 88 | // TODO(thaJeztah): DEPRECATED: remove in v29.1 or v30 |
| 89 | flags.Bool("disable-content-trust", true, "Skip image verification (deprecated)") |
| 90 | _ = flags.MarkDeprecated("disable-content-trust", "support for docker content trust was removed") |
| 91 | copts = addFlags(flags) |
| 92 | |
| 93 | addCompletions(cmd, dockerCLI) |
| 94 | |
| 95 | return cmd |
| 96 | } |
| 97 | |
| 98 | func runCreate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, options *createOptions, copts *containerOptions) error { |
| 99 | if err := validatePullOpt(options.pull); err != nil { |
searching dependent graphs…