github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/exec.go (about) 1 package main 2 3 import ( 4 "github.com/containers/libpod/cmd/podman/cliconfig" 5 "github.com/containers/libpod/pkg/adapter" 6 "github.com/pkg/errors" 7 "github.com/spf13/cobra" 8 ) 9 10 var ( 11 execCommand cliconfig.ExecValues 12 13 execDescription = `Execute the specified command inside a running container. 14 ` 15 _execCommand = &cobra.Command{ 16 Use: "exec [flags] CONTAINER [COMMAND [ARG...]]", 17 Short: "Run a process in a running container", 18 Long: execDescription, 19 RunE: func(cmd *cobra.Command, args []string) error { 20 execCommand.InputArgs = args 21 execCommand.GlobalFlags = MainGlobalOpts 22 execCommand.Remote = remoteclient 23 return execCmd(&execCommand) 24 }, 25 Example: `podman exec -it ctrID ls 26 podman exec -it -w /tmp myCtr pwd 27 podman exec --user root ctrID ls`, 28 } 29 ) 30 31 func init() { 32 execCommand.Command = _execCommand 33 execCommand.SetHelpTemplate(HelpTemplate()) 34 execCommand.SetUsageTemplate(UsageTemplate()) 35 flags := execCommand.Flags() 36 flags.SetInterspersed(false) 37 flags.StringVar(&execCommand.DetachKeys, "detach-keys", getDefaultDetachKeys(), "Select the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _") 38 flags.StringArrayVarP(&execCommand.Env, "env", "e", []string{}, "Set environment variables") 39 flags.StringSliceVar(&execCommand.EnvFile, "env-file", []string{}, "Read in a file of environment variables") 40 flags.BoolVarP(&execCommand.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached") 41 flags.BoolVarP(&execCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of") 42 flags.BoolVar(&execCommand.Privileged, "privileged", false, "Give the process extended Linux capabilities inside the container. The default is false") 43 flags.BoolVarP(&execCommand.Tty, "tty", "t", false, "Allocate a pseudo-TTY. The default is false") 44 flags.StringVarP(&execCommand.User, "user", "u", "", "Sets the username or UID used and optionally the groupname or GID for the specified command") 45 46 flags.IntVar(&execCommand.PreserveFDs, "preserve-fds", 0, "Pass N additional file descriptors to the container") 47 flags.StringVarP(&execCommand.Workdir, "workdir", "w", "", "Working directory inside the container") 48 markFlagHiddenForRemoteClient("env-file", flags) 49 markFlagHiddenForRemoteClient("latest", flags) 50 markFlagHiddenForRemoteClient("preserve-fds", flags) 51 } 52 53 func execCmd(c *cliconfig.ExecValues) error { 54 argLen := len(c.InputArgs) 55 if c.Latest { 56 if argLen < 1 { 57 return errors.Errorf("you must provide a command to exec") 58 } 59 } else { 60 if argLen < 1 { 61 return errors.Errorf("you must provide one container name or id") 62 } 63 if argLen < 2 { 64 return errors.Errorf("you must provide a command to exec") 65 } 66 } 67 runtime, err := adapter.GetRuntimeNoStore(getContext(), &c.PodmanCommand) 68 if err != nil { 69 return errors.Wrapf(err, "error creating libpod runtime") 70 } 71 defer runtime.DeferredShutdown(false) 72 73 exitCode, err = runtime.ExecContainer(getContext(), c) 74 return err 75 }