github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/run.go (about) 1 package main 2 3 import ( 4 "os" 5 6 "github.com/containers/libpod/cmd/podman/cliconfig" 7 "github.com/containers/libpod/pkg/adapter" 8 "github.com/opentracing/opentracing-go" 9 "github.com/pkg/errors" 10 "github.com/sirupsen/logrus" 11 "github.com/spf13/cobra" 12 ) 13 14 var ( 15 runCommand cliconfig.RunValues 16 17 runDescription = "Runs a command in a new container from the given image" 18 _runCommand = &cobra.Command{ 19 Use: "run [flags] IMAGE [COMMAND [ARG...]]", 20 Short: "Run a command in a new container", 21 Long: runDescription, 22 RunE: func(cmd *cobra.Command, args []string) error { 23 runCommand.InputArgs = args 24 runCommand.GlobalFlags = MainGlobalOpts 25 runCommand.Remote = remoteclient 26 return runCmd(&runCommand) 27 }, 28 Example: `podman run imageID ls -alF /etc 29 podman run --network=host imageID dnf -y install java 30 podman run --volume /var/hostdir:/var/ctrdir -i -t fedora /bin/bash`, 31 } 32 ) 33 34 func init() { 35 runCommand.Command = _runCommand 36 runCommand.SetHelpTemplate(HelpTemplate()) 37 runCommand.SetUsageTemplate(UsageTemplate()) 38 flags := runCommand.Flags() 39 flags.SetInterspersed(false) 40 flags.SetNormalizeFunc(aliasFlags) 41 flags.Bool("sig-proxy", true, "Proxy received signals to the process") 42 flags.Bool("rmi", false, "Remove container image unless used by other containers") 43 flags.AddFlagSet(getNetFlags()) 44 getCreateFlags(&runCommand.PodmanCommand) 45 markFlagHiddenForRemoteClient("authfile", flags) 46 } 47 48 func runCmd(c *cliconfig.RunValues) error { 49 if !remote && c.Bool("trace") { 50 span, _ := opentracing.StartSpanFromContext(Ctx, "runCmd") 51 defer span.Finish() 52 } 53 if c.String("authfile") != "" { 54 if _, err := os.Stat(c.String("authfile")); err != nil { 55 return errors.Wrapf(err, "error checking authfile path %s", c.String("authfile")) 56 } 57 } 58 if err := createInit(&c.PodmanCommand); err != nil { 59 return err 60 } 61 62 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 63 if err != nil { 64 return errors.Wrapf(err, "error creating libpod runtime") 65 } 66 defer runtime.DeferredShutdown(false) 67 68 exitCode, err = runtime.Run(getContext(), c, exitCode) 69 if c.Bool("rmi") { 70 imageName := c.InputArgs[0] 71 if newImage, newImageErr := runtime.NewImageFromLocal(imageName); newImageErr != nil { 72 logrus.Errorf("%s", errors.Wrapf(newImageErr, "failed creating image object")) 73 } else if _, errImage := runtime.RemoveImage(getContext(), newImage, false); errImage != nil { 74 logrus.Errorf("%s", errors.Wrapf(errImage, "failed removing image")) 75 } 76 } 77 return err 78 }