github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/create.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/containers/libpod/cmd/podman/cliconfig" 9 "github.com/containers/libpod/pkg/adapter" 10 "github.com/opentracing/opentracing-go" 11 "github.com/pkg/errors" 12 "github.com/sirupsen/logrus" 13 "github.com/spf13/cobra" 14 ) 15 16 var ( 17 createCommand cliconfig.CreateValues 18 createDescription = `Creates a new container from the given image or storage and prepares it for running the specified command. 19 20 The container ID is then printed to stdout. You can then start it at any time with the podman start <container_id> command. The container will be created with the initial state 'created'.` 21 _createCommand = &cobra.Command{ 22 Use: "create [flags] IMAGE [COMMAND [ARG...]]", 23 Short: "Create but do not start a container", 24 Long: createDescription, 25 RunE: func(cmd *cobra.Command, args []string) error { 26 createCommand.InputArgs = args 27 createCommand.GlobalFlags = MainGlobalOpts 28 createCommand.Remote = remoteclient 29 return createCmd(&createCommand) 30 }, 31 Example: `podman create alpine ls 32 podman create --annotation HELLO=WORLD alpine ls 33 podman create -t -i --name myctr alpine ls`, 34 } 35 ) 36 37 func init() { 38 createCommand.PodmanCommand.Command = _createCommand 39 createCommand.SetHelpTemplate(HelpTemplate()) 40 createCommand.SetUsageTemplate(UsageTemplate()) 41 getCreateFlags(&createCommand.PodmanCommand) 42 flags := createCommand.Flags() 43 flags.AddFlagSet(getNetFlags()) 44 flags.SetInterspersed(false) 45 flags.SetNormalizeFunc(aliasFlags) 46 } 47 48 func createCmd(c *cliconfig.CreateValues) error { 49 if c.Bool("trace") { 50 span, _ := opentracing.StartSpanFromContext(Ctx, "createCmd") 51 defer span.Finish() 52 } 53 54 if c.String("authfile") != "" { 55 if _, err := os.Stat(c.String("authfile")); err != nil { 56 return errors.Wrapf(err, "error getting authfile %s", c.String("authfile")) 57 } 58 } 59 60 if err := createInit(&c.PodmanCommand); err != nil { 61 return err 62 } 63 64 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 65 if err != nil { 66 return errors.Wrapf(err, "error creating libpod runtime") 67 } 68 defer runtime.DeferredShutdown(false) 69 70 cid, err := runtime.CreateContainer(getContext(), c) 71 if err != nil { 72 return err 73 } 74 fmt.Printf("%s\n", cid) 75 return nil 76 } 77 78 func createInit(c *cliconfig.PodmanCommand) error { 79 if !remote && c.Bool("trace") { 80 span, _ := opentracing.StartSpanFromContext(Ctx, "createInit") 81 defer span.Finish() 82 } 83 84 if c.IsSet("privileged") && c.IsSet("security-opt") { 85 logrus.Warn("setting security options with --privileged has no effect") 86 } 87 88 if (c.IsSet("dns") || c.IsSet("dns-opt") || c.IsSet("dns-search")) && (c.String("network") == "none" || strings.HasPrefix(c.String("network"), "container:")) { 89 return errors.Errorf("conflicting options: dns and the network mode.") 90 } 91 92 // Docker-compatibility: the "-h" flag for run/create is reserved for 93 // the hostname (see https://github.com/containers/libpod/issues/1367). 94 95 if len(c.InputArgs) < 1 { 96 return errors.Errorf("image name or ID is required") 97 } 98 99 return nil 100 }