Skip to content

Commit 0eac57e

Browse files
committed
podman start: remove container if needed
Like podman run --rm, start --attach must also ensure the contianer is removed before it exist. Otherwise there is a race where the container still exist after the command exits, because removal would only happen by the cleanup process in the background. Signed-off-by: Paul Holzinger <[email protected]>
1 parent 3a2d758 commit 0eac57e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pkg/domain/infra/abi/containers.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,12 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
958958
for i := range containers {
959959
ctr := containers[i]
960960

961+
removeContainer := func() {
962+
if _, _, err := ic.removeContainer(ctx, ctr.Container, entities.RmOptions{}); err != nil {
963+
logrus.Errorf("Removing container %s: %v", ctr.ID(), err)
964+
}
965+
}
966+
961967
if options.Attach {
962968
err = terminal.StartAttachCtr(ctx, ctr.Container, options.Stdout, options.Stderr, options.Stdin, options.DetachKeys, options.SigProxy, true)
963969
if errors.Is(err, define.ErrDetach) {
@@ -991,9 +997,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
991997
ExitCode: exitCode,
992998
})
993999
if ctr.AutoRemove() {
994-
if _, _, err := ic.removeContainer(ctx, ctr.Container, entities.RmOptions{}); err != nil {
995-
logrus.Errorf("Removing container %s: %v", ctr.ID(), err)
996-
}
1000+
removeContainer()
9971001
}
9981002
return reports, fmt.Errorf("unable to start container %s: %w", ctr.ID(), err)
9991003
}
@@ -1002,6 +1006,9 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
10021006
if err2 != nil {
10031007
logrus.Errorf("Waiting for container %s: %v", ctr.ID(), err2)
10041008
}
1009+
if ctr.AutoRemove() && !ctr.ShouldRestart(ctx) {
1010+
removeContainer()
1011+
}
10051012
reports = append(reports, &entities.ContainerStartReport{
10061013
Id: ctr.ID(),
10071014
RawInput: ctr.rawInput,
@@ -1038,9 +1045,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
10381045
}
10391046
report.Err = fmt.Errorf("unable to start container %q: %w", ctr.ID(), err)
10401047
if ctr.AutoRemove() {
1041-
if _, _, err := ic.removeContainer(ctx, ctr.Container, entities.RmOptions{}); err != nil {
1042-
logrus.Errorf("Removing container %s: %v", ctr.ID(), err)
1043-
}
1048+
removeContainer()
10441049
}
10451050
reports = append(reports, report)
10461051
continue

0 commit comments

Comments
 (0)