mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-08-08 01:45:43 +00:00
podman container clone takes the id of an existing continer and creates a specgen from the given container's config recreating all proper namespaces and overriding spec options like resource limits and the container name if given in the cli options this command utilizes the common function DefineCreateFlags meaning that we can funnel as many create options as we want into clone over time allowing the user to clone with as much or as little of the original config as they want. container clone takes a second argument which is a new name and a third argument which is an image name to use instead of the original container's the current supported flags are: --destroy (remove the original container) --name (new ctr name) --cpus (sets cpu period and quota) --cpuset-cpus --cpu-period --cpu-rt-period --cpu-rt-runtime --cpu-shares --cpuset-mems --memory --run resolves #10875 Signed-off-by: cdoern <cdoern@redhat.com> Signed-off-by: cdoern <cbdoer23@g.holycross.edu> Signed-off-by: cdoern <cdoern@redhat.com>
80 lines
2 KiB
Go
80 lines
2 KiB
Go
package containers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/containers/podman/v4/cmd/podman/common"
|
|
"github.com/containers/podman/v4/cmd/podman/registry"
|
|
"github.com/containers/podman/v4/libpod/define"
|
|
"github.com/containers/podman/v4/pkg/domain/entities"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
cloneDescription = `Creates a copy of an existing container.`
|
|
|
|
containerCloneCommand = &cobra.Command{
|
|
Use: "clone [options] CONTAINER NAME IMAGE",
|
|
Short: "Clone an existing container",
|
|
Long: cloneDescription,
|
|
RunE: clone,
|
|
Args: cobra.RangeArgs(1, 3),
|
|
ValidArgsFunction: common.AutocompleteClone,
|
|
Example: `podman container clone container_name new_name image_name`,
|
|
}
|
|
)
|
|
|
|
var (
|
|
ctrClone entities.ContainerCloneOptions
|
|
)
|
|
|
|
func cloneFlags(cmd *cobra.Command) {
|
|
flags := cmd.Flags()
|
|
|
|
destroyFlagName := "destroy"
|
|
flags.BoolVar(&ctrClone.Destroy, destroyFlagName, false, "destroy the original container")
|
|
|
|
runFlagName := "run"
|
|
flags.BoolVar(&ctrClone.Run, runFlagName, false, "run the new container")
|
|
|
|
common.DefineCreateFlags(cmd, &ctrClone.CreateOpts, false, true)
|
|
}
|
|
func init() {
|
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
|
Command: containerCloneCommand,
|
|
Parent: containerCmd,
|
|
})
|
|
|
|
cloneFlags(containerCloneCommand)
|
|
}
|
|
|
|
func clone(cmd *cobra.Command, args []string) error {
|
|
switch len(args) {
|
|
case 0:
|
|
return errors.Wrapf(define.ErrInvalidArg, "Must Specify at least 1 argument")
|
|
case 2:
|
|
ctrClone.CreateOpts.Name = args[1]
|
|
case 3:
|
|
ctrClone.CreateOpts.Name = args[1]
|
|
ctrClone.Image = args[2]
|
|
rawImageName := ""
|
|
if !cliVals.RootFS {
|
|
rawImageName = args[0]
|
|
name, err := PullImage(ctrClone.Image, ctrClone.CreateOpts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctrClone.Image = name
|
|
ctrClone.RawImageName = rawImageName
|
|
}
|
|
}
|
|
ctrClone.ID = args[0]
|
|
ctrClone.CreateOpts.IsClone = true
|
|
rep, err := registry.ContainerEngine().ContainerClone(registry.GetContext(), ctrClone)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(rep.Id)
|
|
return nil
|
|
}
|