MCPcopy
hub / github.com/docker/compose / execCommand

Function execCommand

cmd/compose/exec.go:52–102  ·  view source on GitHub ↗
(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions)

Source from the content-addressed store, hash-verified

50}
51
52func execCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
53 opts := execOpts{
54 composeOptions: &composeOptions{
55 ProjectOptions: p,
56 },
57 }
58 runCmd := &cobra.Command{
59 Use: "exec [OPTIONS] SERVICE COMMAND [ARGS...]",
60 Short: "Execute a command in a running container",
61 Args: cobra.MinimumNArgs(2),
62 PreRunE: Adapt(func(ctx context.Context, args []string) error {
63 opts.service = args[0]
64 opts.command = args[1:]
65 return nil
66 }),
67 RunE: Adapt(func(ctx context.Context, args []string) error {
68 err := runExec(ctx, dockerCli, backendOptions, opts)
69 if err != nil {
70 logrus.Debugf("%v", err)
71 var cliError cli.StatusError
72 if ok := errors.As(err, &cliError); ok {
73 os.Exit(err.(cli.StatusError).StatusCode) //nolint: errorlint
74 }
75 }
76 return err
77 }),
78 ValidArgsFunction: completeServiceNames(dockerCli, p),
79 }
80
81 runCmd.Flags().BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: Run command in the background")
82 runCmd.Flags().StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
83 runCmd.Flags().IntVar(&opts.index, "index", 0, "Index of the container if service has multiple replicas")
84 runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process")
85 runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user")
86 runCmd.Flags().BoolVarP(&opts.noTty, "no-tty", "T", !dockerCli.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default 'docker compose exec' allocates a TTY.")
87 runCmd.Flags().StringVarP(&opts.workingDir, "workdir", "w", "", "Path to workdir directory for this command")
88
89 runCmd.Flags().BoolVarP(&opts.interactive, "interactive", "i", true, "Keep STDIN open even if not attached")
90 runCmd.Flags().MarkHidden("interactive") //nolint:errcheck
91 runCmd.Flags().BoolP("tty", "t", true, "Allocate a pseudo-TTY")
92 runCmd.Flags().MarkHidden("tty") //nolint:errcheck
93
94 runCmd.Flags().SetInterspersed(false)
95 runCmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
96 if name == "no-TTY" { // legacy
97 name = "no-tty"
98 }
99 return pflag.NormalizedName(name)
100 })
101 return runCmd
102}
103
104func runExec(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts execOpts) error {
105 projectName, err := opts.toProjectName(ctx, dockerCli)

Callers 1

RootCommandFunction · 0.85

Calls 4

AdaptFunction · 0.85
runExecFunction · 0.85
completeServiceNamesFunction · 0.85
OutMethod · 0.45

Tested by

no test coverage detected