diff --git a/cmd/podman/root.go b/cmd/podman/root.go index d0fc0ed05e..bec9bd071e 100644 --- a/cmd/podman/root.go +++ b/cmd/podman/root.go @@ -21,6 +21,7 @@ import ( "github.com/containers/podman/v4/pkg/domain/entities" "github.com/containers/podman/v4/pkg/parallel" "github.com/containers/podman/v4/version" + "github.com/containers/storage" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -632,7 +633,8 @@ func rootFlags(cmd *cobra.Command, podmanConfig *entities.PodmanConfig) { func formatError(err error) string { var message string - if errors.Is(err, define.ErrOCIRuntime) { + switch { + case errors.Is(err, define.ErrOCIRuntime): // OCIRuntimeErrors include the reason for the failure in the // second to last message in the error chain. message = fmt.Sprintf( @@ -640,7 +642,9 @@ func formatError(err error) string { define.ErrOCIRuntime.Error(), strings.TrimSuffix(err.Error(), ": "+define.ErrOCIRuntime.Error()), ) - } else { + case errors.Is(err, storage.ErrDuplicateName): + message = fmt.Sprintf("Error: %s, or use --replace to instruct Podman to do so.", err.Error()) + default: if logrus.IsLevelEnabled(logrus.TraceLevel) { message = fmt.Sprintf("Error: %+v", err) } else { diff --git a/libpod/container_internal.go b/libpod/container_internal.go index f89f334390..48c5c970ca 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -510,6 +510,11 @@ func (c *Container) setupStorage(ctx context.Context) error { } } if containerInfoErr != nil { + if errors.Is(containerInfoErr, storage.ErrDuplicateName) { + if _, err := c.runtime.LookupContainer(c.config.Name); errors.Is(err, define.ErrNoSuchCtr) { + return fmt.Errorf("creating container storage: %w by an external entity", containerInfoErr) + } + } return fmt.Errorf("creating container storage: %w", containerInfoErr) } diff --git a/test/system/030-run.bats b/test/system/030-run.bats index 4aa3462de7..e688e8e833 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -1311,4 +1311,29 @@ search | $IMAGE | run_podman rm -f -t0 $cid } +@test "podman create container with conflicting name" { + local cname="$(random_string 10 | tr A-Z a-z)" + local output_msg_ext="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use by an external entity, or use --replace to instruct Podman to do so." + local output_msg="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use, or use --replace to instruct Podman to do so." + if is_remote; then + output_msg_ext="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use by an external entity" + output_msg="^Error: .*: the container name \"$cname\" is already in use by .* You have to remove that container to be able to reuse that name: that name is already in use" + fi + + # external container + buildah from --name $cname scratch + + run_podman 125 create --name $cname $IMAGE + assert "$output" =~ "$output_msg_ext" "Trying to create two containers with same name" + + run_podman container rm $cname + + run_podman --noout create --name $cname $IMAGE + + run_podman 125 create --name $cname $IMAGE + assert "$output" =~ "$output_msg" "Trying to create two containers with same name" + + run_podman container rm $cname +} + # vim: filetype=sh