github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/start.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/libpod/define"
     6  	"github.com/containers/libpod/pkg/adapter"
     7  	"github.com/opentracing/opentracing-go"
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	startCommand     cliconfig.StartValues
    14  	startDescription = `Starts one or more containers.  The container name or ID can be used.`
    15  
    16  	_startCommand = &cobra.Command{
    17  		Use:   "start [flags] CONTAINER [CONTAINER...]",
    18  		Short: "Start one or more containers",
    19  		Long:  startDescription,
    20  		RunE: func(cmd *cobra.Command, args []string) error {
    21  			startCommand.InputArgs = args
    22  			startCommand.GlobalFlags = MainGlobalOpts
    23  			startCommand.Remote = remoteclient
    24  			return startCmd(&startCommand)
    25  		},
    26  		Example: `podman start --latest
    27    podman start 860a4b231279 5421ab43b45
    28    podman start --interactive --attach imageID`,
    29  	}
    30  )
    31  
    32  func init() {
    33  	startCommand.Command = _startCommand
    34  	startCommand.SetHelpTemplate(HelpTemplate())
    35  	startCommand.SetUsageTemplate(UsageTemplate())
    36  	flags := startCommand.Flags()
    37  	flags.BoolVarP(&startCommand.Attach, "attach", "a", false, "Attach container's STDOUT and STDERR")
    38  	flags.StringVar(&startCommand.DetachKeys, "detach-keys", getDefaultDetachKeys(), "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
    39  	flags.BoolVarP(&startCommand.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
    40  	flags.BoolVarP(&startCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    41  	flags.BoolVar(&startCommand.SigProxy, "sig-proxy", false, "Proxy received signals to the process (default true if attaching, false otherwise)")
    42  	markFlagHiddenForRemoteClient("latest", flags)
    43  }
    44  
    45  func startCmd(c *cliconfig.StartValues) error {
    46  	if !remoteclient && c.Bool("trace") {
    47  		span, _ := opentracing.StartSpanFromContext(Ctx, "startCmd")
    48  		defer span.Finish()
    49  	}
    50  
    51  	args := c.InputArgs
    52  	if len(args) < 1 && !c.Latest {
    53  		return errors.Errorf("you must provide at least one container name or id")
    54  	}
    55  
    56  	attach := c.Attach
    57  
    58  	if len(args) > 1 && attach {
    59  		return errors.Errorf("you cannot start and attach multiple containers at once")
    60  	}
    61  
    62  	sigProxy := c.SigProxy || attach
    63  	if c.Flag("sig-proxy").Changed {
    64  		sigProxy = c.SigProxy
    65  	}
    66  
    67  	if sigProxy && !attach {
    68  		return errors.Wrapf(define.ErrInvalidArg, "you cannot use sig-proxy without --attach")
    69  	}
    70  
    71  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    72  	if err != nil {
    73  		return errors.Wrapf(err, "error creating libpod runtime")
    74  	}
    75  	defer runtime.DeferredShutdown(false)
    76  	exitCode, err = runtime.Start(getContext(), c, sigProxy)
    77  	return err
    78  }