summaryrefslogtreecommitdiff
path: root/cmd/podman
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2020-06-10 16:26:24 +0200
committerValentin Rothberg <rothberg@redhat.com>2020-06-15 15:53:51 +0200
commitfa3b8a75c4ec571f8cbb2622ea624b42bc5c2472 (patch)
tree23b4ebcca03d20cedadfa094d51469d55e756b83 /cmd/podman
parentf4c3b718eb22a161a897a6ed55d10f3a07e31aa8 (diff)
downloadpodman-fa3b8a75c4ec571f8cbb2622ea624b42bc5c2472.tar.gz
podman-fa3b8a75c4ec571f8cbb2622ea624b42bc5c2472.tar.bz2
podman-fa3b8a75c4ec571f8cbb2622ea624b42bc5c2472.zip
{create,run} --replace
Add a `--replace` flag to the `container {create,run}` commands. If another container with the same name already exists, it will be replaced and removed. Adding this flag is motivated by #5485 to make running Podman in systemd units (or any other scripts/automation) more robust. In case of a crash, a container may not be removed by a sytemd unit anymore. The `--replace` flag allows for supporting crashes. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'cmd/podman')
-rw-r--r--cmd/podman/common/create.go5
-rw-r--r--cmd/podman/common/create_opts.go1
-rw-r--r--cmd/podman/containers/create.go17
-rw-r--r--cmd/podman/containers/rm.go16
-rw-r--r--cmd/podman/containers/run.go6
5 files changed, 42 insertions, 3 deletions
diff --git a/cmd/podman/common/create.go b/cmd/podman/common/create.go
index e79c5c20b..921cd5a71 100644
--- a/cmd/podman/common/create.go
+++ b/cmd/podman/common/create.go
@@ -373,6 +373,11 @@ func GetCreateFlags(cf *ContainerCLIOpts) *pflag.FlagSet {
"read-only-tmpfs", true,
"When running containers in read-only mode mount a read-write tmpfs on /run, /tmp and /var/tmp",
)
+ createFlags.BoolVar(
+ &cf.Replace,
+ "replace", false,
+ `If a container with the same name exists, replace it`,
+ )
createFlags.StringVar(
&cf.Restart,
"restart", "",
diff --git a/cmd/podman/common/create_opts.go b/cmd/podman/common/create_opts.go
index 98dc6744c..49052704e 100644
--- a/cmd/podman/common/create_opts.go
+++ b/cmd/podman/common/create_opts.go
@@ -76,6 +76,7 @@ type ContainerCLIOpts struct {
ReadOnly bool
ReadOnlyTmpFS bool
Restart string
+ Replace bool
Rm bool
RootFS bool
SecurityOpt []string
diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go
index ed09585ba..6269ec781 100644
--- a/cmd/podman/containers/create.go
+++ b/cmd/podman/containers/create.go
@@ -122,6 +122,12 @@ func create(cmd *cobra.Command, args []string) error {
return err
}
+ if cliVals.Replace {
+ if err := replaceContainer(cliVals.Name); err != nil {
+ return err
+ }
+ }
+
report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s)
if err != nil {
return err
@@ -138,6 +144,17 @@ func create(cmd *cobra.Command, args []string) error {
return nil
}
+func replaceContainer(name string) error {
+ if len(name) == 0 {
+ return errors.New("cannot replace container without --name being set")
+ }
+ rmOptions := entities.RmOptions{
+ Force: true, // force stop & removal
+ Ignore: true, // ignore errors when a container doesn't exit
+ }
+ return removeContainers([]string{name}, rmOptions, false)
+}
+
func createInit(c *cobra.Command) error {
if c.Flag("privileged").Changed && c.Flag("security-opt").Changed {
logrus.Warn("setting security options with --privileged has no effect")
diff --git a/cmd/podman/containers/rm.go b/cmd/podman/containers/rm.go
index b25473a8d..22d6d59b4 100644
--- a/cmd/podman/containers/rm.go
+++ b/cmd/podman/containers/rm.go
@@ -87,6 +87,14 @@ func init() {
}
func rm(cmd *cobra.Command, args []string) error {
+ return removeContainers(args, rmOptions, true)
+}
+
+// removeContainers will remove the specified containers (names or IDs).
+// Allows for sharing removal logic across commands. If setExit is set,
+// removeContainers will set the exit code according to the `podman-rm` man
+// page.
+func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit bool) error {
var (
errs utils.OutputErrors
)
@@ -96,9 +104,9 @@ func rm(cmd *cobra.Command, args []string) error {
return errors.Errorf("--storage conflicts with --volumes, --all, --latest, --ignore and --cidfile")
}
}
- responses, err := registry.ContainerEngine().ContainerRm(context.Background(), args, rmOptions)
+ responses, err := registry.ContainerEngine().ContainerRm(context.Background(), namesOrIDs, rmOptions)
if err != nil {
- if len(args) < 2 {
+ if setExit && len(namesOrIDs) < 2 {
setExitCode(err)
}
return err
@@ -109,7 +117,9 @@ func rm(cmd *cobra.Command, args []string) error {
if errors.Cause(err) == define.ErrWillDeadlock {
logrus.Errorf("Potential deadlock detected - please run 'podman system renumber' to resolve")
}
- setExitCode(r.Err)
+ if setExit {
+ setExitCode(r.Err)
+ }
errs = append(errs, r.Err)
} else {
fmt.Println(r.Id)
diff --git a/cmd/podman/containers/run.go b/cmd/podman/containers/run.go
index 8a02c63c0..b9c196b64 100644
--- a/cmd/podman/containers/run.go
+++ b/cmd/podman/containers/run.go
@@ -129,6 +129,12 @@ func run(cmd *cobra.Command, args []string) error {
}
}
+ if cliVals.Replace {
+ if err := replaceContainer(cliVals.Name); err != nil {
+ return err
+ }
+ }
+
// If -i is not set, clear stdin
if !cliVals.Interactive {
runOpts.InputStream = nil