Skip to content

Commit

Permalink
With --rm option remove container if podman run fails
Browse files Browse the repository at this point in the history
Fixes containers#15049

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Jul 25, 2022
1 parent da1f479 commit 4d9b757
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/domain/infra/abi/containers.go
Expand Up @@ -1060,6 +1060,15 @@ func (ic *ContainerEngine) Diff(ctx context.Context, namesOrIDs []string, opts e
}

func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.ContainerRunOptions) (*entities.ContainerRunReport, error) {
removeContainer := func(ctr *libpod.Container, force bool) error {
var timeout *uint
if err := ic.Libpod.RemoveContainer(ctx, ctr, force, true, timeout); err != nil {
logrus.Debugf("unable to remove container %s after failing to start and attach to it: %v", ctr.ID(), err)
return err
}
return nil
}

warn, err := generate.CompleteSpec(ctx, ic.Libpod, opts.Spec)
if err != nil {
return nil, err
Expand All @@ -1080,6 +1089,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta

if opts.CIDFile != "" {
if err := util.CreateCidFile(opts.CIDFile, ctr.ID()); err != nil {
_ = removeContainer(ctr, true)
return nil, err
}
}
Expand All @@ -1097,6 +1107,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
if err := ctr.Start(ctx, true); err != nil {
// This means the command did not exist
report.ExitCode = define.ExitCode(err)
_ = removeContainer(ctr, true)
return &report, err
}

Expand All @@ -1113,10 +1124,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
return &report, nil
}
if opts.Rm {
var timeout *uint
if deleteError := ic.Libpod.RemoveContainer(ctx, ctr, true, false, timeout); deleteError != nil {
logrus.Debugf("unable to remove container %s after failing to start and attach to it", ctr.ID())
}
_ = removeContainer(ctr, true)
}
if errors.Is(err, define.ErrWillDeadlock) {
logrus.Debugf("Deadlock error on %q: %v", ctr.ID(), err)
Expand All @@ -1128,8 +1136,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
}
report.ExitCode = ic.GetContainerExitCode(ctx, ctr)
if opts.Rm && !ctr.ShouldRestart(ctx) {
var timeout *uint
if err := ic.Libpod.RemoveContainer(ctx, ctr, false, true, timeout); err != nil {
if err := removeContainer(ctr, false); err != nil {
if errors.Is(err, define.ErrNoSuchCtr) ||
errors.Is(err, define.ErrCtrRemoved) {
logrus.Infof("Container %s was already removed, skipping --rm", ctr.ID())
Expand Down
14 changes: 14 additions & 0 deletions test/system/030-run.bats
Expand Up @@ -870,4 +870,18 @@ EOF
run_podman container rm -fa
}

@test "podman run failed --rm " {
rand=$(random_string 30)
run_podman run -p 1200:80 --rm -d --name $rand $IMAGE
run_podman 125 run -p 1200:80 --rm -d $IMAGE
run_podman ps -a
is "${#lines[@]}" "1" "podman ps -a should only see 1 container or rm failed"
run_podman stop test
run_podman wait test

# containers should not be in output of 'ps -a'
run_podman ps -a
is "$output" "" "Container should not be present in 'ps -a'"
}

# vim: filetype=sh

0 comments on commit 4d9b757

Please sign in to comment.