newExecCommand creates a new cobra.Command for "docker exec".
(dockerCLI command.Cli)
| 41 | |
| 42 | // newExecCommand creates a new cobra.Command for "docker exec". |
| 43 | func newExecCommand(dockerCLI command.Cli) *cobra.Command { |
| 44 | options := NewExecOptions() |
| 45 | |
| 46 | cmd := &cobra.Command{ |
| 47 | Use: "exec [OPTIONS] CONTAINER COMMAND [ARG...]", |
| 48 | Short: "Execute a command in a running container", |
| 49 | Args: cli.RequiresMinArgs(2), |
| 50 | RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | containerIDorName := args[0] |
| 52 | options.Command = args[1:] |
| 53 | return RunExec(cmd.Context(), dockerCLI, containerIDorName, options) |
| 54 | }, |
| 55 | ValidArgsFunction: completion.ContainerNames(dockerCLI, false, func(ctr container.Summary) bool { |
| 56 | return ctr.State != container.StatePaused |
| 57 | }), |
| 58 | Annotations: map[string]string{ |
| 59 | "category-top": "2", |
| 60 | "aliases": "docker container exec, docker exec", |
| 61 | }, |
| 62 | DisableFlagsInUseLine: true, |
| 63 | } |
| 64 | |
| 65 | flags := cmd.Flags() |
| 66 | flags.SetInterspersed(false) |
| 67 | |
| 68 | flags.StringVar(&options.DetachKeys, "detach-keys", "", "Override the key sequence for detaching a container") |
| 69 | flags.BoolVarP(&options.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached") |
| 70 | flags.BoolVarP(&options.TTY, "tty", "t", false, "Allocate a pseudo-TTY") |
| 71 | flags.BoolVarP(&options.Detach, "detach", "d", false, "Detached mode: run command in the background") |
| 72 | flags.StringVarP(&options.User, "user", "u", "", `Username or UID (format: "<name|uid>[:<group|gid>]")`) |
| 73 | flags.BoolVar(&options.Privileged, "privileged", false, "Give extended privileges to the command") |
| 74 | flags.VarP(&options.Env, "env", "e", "Set environment variables") |
| 75 | _ = flags.SetAnnotation("env", "version", []string{"1.25"}) |
| 76 | flags.Var(&options.EnvFile, "env-file", "Read in a file of environment variables") |
| 77 | _ = flags.SetAnnotation("env-file", "version", []string{"1.25"}) |
| 78 | flags.StringVarP(&options.Workdir, "workdir", "w", "", "Working directory inside the container") |
| 79 | _ = flags.SetAnnotation("workdir", "version", []string{"1.35"}) |
| 80 | |
| 81 | _ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames()) |
| 82 | _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames()) |
| 83 | |
| 84 | return cmd |
| 85 | } |
| 86 | |
| 87 | // RunExec executes an `exec` command |
| 88 | func RunExec(ctx context.Context, dockerCLI command.Cli, containerIDorName string, options ExecOptions) error { |
searching dependent graphs…