github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/commit.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "strings" 7 8 "github.com/containers/libpod/cmd/podman/cliconfig" 9 "github.com/containers/libpod/pkg/adapter" 10 "github.com/pkg/errors" 11 "github.com/spf13/cobra" 12 ) 13 14 var ( 15 commitCommand cliconfig.CommitValues 16 commitDescription = `Create an image from a container's changes. Optionally tag the image created, set the author with the --author flag, set the commit message with the --message flag, and make changes to the instructions with the --change flag.` 17 18 _commitCommand = &cobra.Command{ 19 Use: "commit [flags] CONTAINER [IMAGE]", 20 Short: "Create new image based on the changed container", 21 Long: commitDescription, 22 RunE: func(cmd *cobra.Command, args []string) error { 23 commitCommand.InputArgs = args 24 commitCommand.GlobalFlags = MainGlobalOpts 25 commitCommand.Remote = remoteclient 26 return commitCmd(&commitCommand) 27 }, 28 Example: `podman commit -q --message "committing container to image" reverent_golick image-committed 29 podman commit -q --author "firstName lastName" reverent_golick image-committed 30 podman commit -q --pause=false containerID image-committed 31 podman commit containerID`, 32 } 33 34 // ChangeCmds is the list of valid Changes commands to passed to the Commit call 35 ChangeCmds = []string{"CMD", "ENTRYPOINT", "ENV", "EXPOSE", "LABEL", "ONBUILD", "STOPSIGNAL", "USER", "VOLUME", "WORKDIR"} 36 ) 37 38 func init() { 39 commitCommand.Command = _commitCommand 40 commitCommand.SetHelpTemplate(HelpTemplate()) 41 commitCommand.SetUsageTemplate(UsageTemplate()) 42 flags := commitCommand.Flags() 43 flags.StringArrayVarP(&commitCommand.Change, "change", "c", []string{}, fmt.Sprintf("Apply the following possible instructions to the created image (default []): %s", strings.Join(ChangeCmds, " | "))) 44 flags.StringVarP(&commitCommand.Format, "format", "f", "oci", "`Format` of the image manifest and metadata") 45 flags.StringVarP(&commitCommand.ImageIDFile, "iidfile", "", "", "`file` to write the image ID to") 46 flags.StringVarP(&commitCommand.Message, "message", "m", "", "Set commit message for imported image") 47 flags.StringVarP(&commitCommand.Author, "author", "a", "", "Set the author for the image committed") 48 flags.BoolVarP(&commitCommand.Pause, "pause", "p", false, "Pause container during commit") 49 flags.BoolVarP(&commitCommand.Quiet, "quiet", "q", false, "Suppress output") 50 flags.BoolVar(&commitCommand.IncludeVolumes, "include-volumes", false, "Include container volumes as image volumes") 51 } 52 53 func commitCmd(c *cliconfig.CommitValues) error { 54 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 55 if err != nil { 56 return errors.Wrapf(err, "could not get runtime") 57 } 58 defer runtime.DeferredShutdown(false) 59 60 args := c.InputArgs 61 if len(args) < 1 { 62 return errors.Errorf("you must provide a container name or ID and optionally a target image name") 63 } 64 65 container := args[0] 66 reference := "" 67 if len(args) > 1 { 68 reference = args[1] 69 } 70 71 iid, err := runtime.Commit(getContext(), c, container, reference) 72 if err != nil { 73 return err 74 } 75 if c.ImageIDFile != "" { 76 if err = ioutil.WriteFile(c.ImageIDFile, []byte(iid), 0644); err != nil { 77 return errors.Wrapf(err, "failed to write image ID to file %q", c.ImageIDFile) 78 } 79 } 80 fmt.Println(iid) 81 return nil 82 }