From 2d65e57ae69e83ae0007562cfd121a5f35648950 Mon Sep 17 00:00:00 2001 From: Chetan Giradkar Date: Mon, 16 Oct 2023 11:29:32 +0100 Subject: [PATCH] Define better error message for container name conflicts with external storage. Updated the error message to suggest user to use --replace option to instruct Podman to replace the existsing external container with a newly created one. closes #16759 Signed-off-by: Chetan Giradkar --- cmd/podman/root.go | 8 ++++++-- libpod/container_internal.go | 5 +++++ test/system/030-run.bats | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/cmd/podman/root.go b/cmd/podman/root.go index 484ea46a99..27acfcff9f 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" @@ -626,7 +627,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( @@ -634,7 +636,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 6e8306a240..d8f95785d5 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -496,6 +496,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 5c6751d35c..d0a19470fe 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -1303,4 +1303,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