diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go index 8fe845d44994..d1b0070613f8 100644 --- a/cmd/podman/containers/create.go +++ b/cmd/podman/containers/create.go @@ -180,6 +180,13 @@ func create(cmd *cobra.Command, args []string) error { report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s) if err != nil { + // if pod was created as part of run + // remove it in case ctr creation fails + if err := rmPodIfNecessary(cmd, s); err != nil { + if !errors.Is(err, define.ErrNoSuchPod) { + logrus.Error(err.Error()) + } + } return err } @@ -385,6 +392,18 @@ func PullImage(imageName string, cliVals *entities.ContainerCreateOptions) (stri return imageName, nil } +func rmPodIfNecessary(cmd *cobra.Command, s *specgen.SpecGenerator) error { + if !strings.HasPrefix(cmd.Flag("pod").Value.String(), "new:") { + return nil + } + + // errcheck not necessary since + // pod creation would've failed + podName := strings.Replace(s.Pod, "new:", "", 1) + _, err := registry.ContainerEngine().PodRm(context.Background(), []string{podName}, entities.PodRmOptions{}) + return err +} + // createPodIfNecessary automatically creates a pod when requested. if the pod name // has the form new:ID, the pod ID is created and the name in the spec generator is replaced // with ID. diff --git a/cmd/podman/containers/run.go b/cmd/podman/containers/run.go index 3ce6ada180d1..46d778c402fe 100644 --- a/cmd/podman/containers/run.go +++ b/cmd/podman/containers/run.go @@ -228,6 +228,13 @@ func run(cmd *cobra.Command, args []string) error { registry.SetExitCode(report.ExitCode) } if err != nil { + // if pod was created as part of run + // remove it in case ctr creation fails + if err := rmPodIfNecessary(cmd, s); err != nil { + if !errors.Is(err, define.ErrNoSuchPod) { + logrus.Error(err.Error()) + } + } return err } diff --git a/test/system/030-run.bats b/test/system/030-run.bats index 057b1d646cf7..064240450196 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -1399,4 +1399,20 @@ search | $IMAGE | run_podman rm -f -t0 $cid } +@test "podman run - rm pod if container creation failed with -pod new:" { + run_podman run -d --name foobar $IMAGE hostname + cid=$output + + podname=pod$(random_string) + run_podman 125 run --rm --pod "new:$podname" --name foobar $IMAGE hostname + is "$output" ".*creating container storage: the container name \"foobar\" is already in use by" + + # pod should've been cleaned up + # if container creation failed + run_podman 1 pod exists $podname + + run_podman rm $cid + run_podman rmi $(pause_image) +} + # vim: filetype=sh diff --git a/test/system/danish.bats b/test/system/danish.bats new file mode 100644 index 000000000000..53c847fd5011 --- /dev/null +++ b/test/system/danish.bats @@ -0,0 +1,22 @@ +#!/usr/bin/env bats + +load helpers +load helpers.network + + + +@test "podman run - rm pod if container creation failed with -pod new:" { + run_podman run -d --name foobar $IMAGE hostname + cid=$output + + podname=pod$(random_string) + run_podman 125 run --rm --pod "new:$podname" --name foobar $IMAGE hostname + is "$output" ".*creating container storage: the container name \"foobar\" is already in use by" + + # pod should've been cleaned up + # if container creation failed + run_podman 1 pod exists $podname + + run_podman rm $cid + run_podman rmi $(pause_image) +}