summaryrefslogtreecommitdiff
path: root/pkg/domain/infra/abi/containers.go
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2021-07-15 14:56:58 +0200
committerValentin Rothberg <rothberg@redhat.com>2021-07-16 10:57:40 +0200
commit9924c57d4c42b596306c21c601d0f970b2735e5e (patch)
treef8f8545d86b1c3511b8607c45075d32932a391cd /pkg/domain/infra/abi/containers.go
parentf0cd16cb32676eaf5ee337b50e8628f2f49ccce9 (diff)
downloadpodman-9924c57d4c42b596306c21c601d0f970b2735e5e.tar.gz
podman-9924c57d4c42b596306c21c601d0f970b2735e5e.tar.bz2
podman-9924c57d4c42b596306c21c601d0f970b2735e5e.zip
podman start: remove containers configured for auto removal
Make sure that containers configured for auto removal (e.g., via `podman create --rm`) are removed in `podman start` if starting the container failed. Fixes: #10935 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'pkg/domain/infra/abi/containers.go')
-rw-r--r--pkg/domain/infra/abi/containers.go47
1 files changed, 29 insertions, 18 deletions
diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go
index 2c5300ccb..50751aa12 100644
--- a/pkg/domain/infra/abi/containers.go
+++ b/pkg/domain/infra/abi/containers.go
@@ -261,6 +261,24 @@ func (ic *ContainerEngine) ContainerRestart(ctx context.Context, namesOrIds []st
return reports, nil
}
+func (ic *ContainerEngine) removeContainer(ctx context.Context, ctr *libpod.Container, options entities.RmOptions) error {
+ err := ic.Libpod.RemoveContainer(ctx, ctr, options.Force, options.Volumes)
+ if err == nil {
+ return nil
+ }
+ logrus.Debugf("Failed to remove container %s: %s", ctr.ID(), err.Error())
+ switch errors.Cause(err) {
+ case define.ErrNoSuchCtr:
+ if options.Ignore {
+ logrus.Debugf("Ignoring error (--allow-missing): %v", err)
+ return nil
+ }
+ case define.ErrCtrRemoved:
+ return nil
+ }
+ return err
+}
+
func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, options entities.RmOptions) ([]*entities.RmReport, error) {
reports := []*entities.RmReport{}
@@ -318,21 +336,7 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string,
}
errMap, err := parallelctr.ContainerOp(ctx, ctrs, func(c *libpod.Container) error {
- err := ic.Libpod.RemoveContainer(ctx, c, options.Force, options.Volumes)
- if err == nil {
- return nil
- }
- logrus.Debugf("Failed to remove container %s: %s", c.ID(), err.Error())
- switch errors.Cause(err) {
- case define.ErrNoSuchCtr:
- if options.Ignore {
- logrus.Debugf("Ignoring error (--allow-missing): %v", err)
- return nil
- }
- case define.ErrCtrRemoved:
- return nil
- }
- return err
+ return ic.removeContainer(ctx, c, options)
})
if err != nil {
return nil, err
@@ -791,6 +795,11 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
Err: err,
ExitCode: exitCode,
})
+ if ctr.AutoRemove() {
+ if err := ic.removeContainer(ctx, ctr, entities.RmOptions{}); err != nil {
+ logrus.Errorf("Error removing container %s: %v", ctr.ID(), err)
+ }
+ }
return reports, errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
@@ -827,9 +836,6 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
ExitCode: 125,
}
if err := ctr.Start(ctx, true); err != nil {
- // if lastError != nil {
- // fmt.Fprintln(os.Stderr, lastError)
- // }
report.Err = err
if errors.Cause(err) == define.ErrWillDeadlock {
report.Err = errors.Wrapf(err, "please run 'podman system renumber' to resolve deadlocks")
@@ -838,6 +844,11 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
}
report.Err = errors.Wrapf(err, "unable to start container %q", ctr.ID())
reports = append(reports, report)
+ if ctr.AutoRemove() {
+ if err := ic.removeContainer(ctx, ctr, entities.RmOptions{}); err != nil {
+ logrus.Errorf("Error removing container %s: %v", ctr.ID(), err)
+ }
+ }
continue
}
report.ExitCode = 0