{"record":{"id":"949bb3fefbd5426f","repo":"docker/cli","slug":"failed-to-rename-container-w","errorCode":null,"errorMessage":"failed to rename container: %w","messagePattern":"failed to rename container: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/container/rename.go","lineNumber":25,"sourceCode":"\t\"github.com/docker/cli/cli/command\"\n\t\"github.com/docker/cli/cli/command/completion\"\n\t\"github.com/moby/moby/client\"\n\t\"github.com/spf13/cobra\"\n)\n\n// newRenameCommand creates a new cobra.Command for \"docker container rename\".\nfunc newRenameCommand(dockerCLI command.Cli) *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse:   \"rename CONTAINER NEW_NAME\",\n\t\tShort: \"Rename a container\",\n\t\tArgs:  cli.ExactArgs(2),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\toldName, newName := args[0], args[1]\n\t\t\t_, err := dockerCLI.Client().ContainerRename(cmd.Context(), oldName, client.ContainerRenameOptions{\n\t\t\t\tNewName: newName,\n\t\t\t})\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to rename container: %w\", err)\n\t\t\t}\n\t\t\treturn nil\n\t\t},\n\t\tAnnotations: map[string]string{\n\t\t\t\"aliases\": \"docker container rename, docker rename\",\n\t\t},\n\t\tValidArgsFunction:     completion.ContainerNames(dockerCLI, true),\n\t\tDisableFlagsInUseLine: true,\n\t}\n\treturn cmd\n}\n","sourceCodeStart":7,"sourceCodeEnd":37,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/rename.go#L7-L37","documentation":"Thrown by newRenameCommand when the client.ContainerRename API call returns an error. The underlying error is wrapped, so it typically carries a daemon message such as 'container already exists', 'no such container', or 'conflict'.","triggerScenarios":"Running `docker rename old new` where 'old' does not exist, 'new' is already taken, the name is invalid, or the daemon rejects the request (permission, name conflict with an existing container ID prefix).","commonSituations":"Target name already used by another container; source name typo; invalid characters in the new name; daemon in read-only/swarm-worker mode; name conflict after a partial cleanup.","solutions":["Confirm the source exists: docker ps -a --filter name=^/old$","Pick a unique new name: docker ps -a --filter name=^/new$ (should be empty)","Remove or rename the conflicting container first","Use valid characters: alphanumeric, underscore, dot, hyphen"],"exampleFix":"# before\ndocker rename oldctr existingctr  # existingctr already in use\n# after\ndocker rename oldctr newctr","handlingStrategy":"try-catch","validationCode":"// Confirm source exists and target name is free before renaming.\nif _, err := cli.ContainerInspect(ctx, oldName); err != nil {\n    return fmt.Errorf(\"source container missing: %w\", err)\n}\nif _, err := cli.ContainerInspect(ctx, newName); err == nil {\n    return fmt.Errorf(\"target name %q already in use\", newName)\n}","typeGuard":null,"tryCatchPattern":"if _, err := cli.ContainerRename(ctx, oldName, client.ContainerRenameOptions{NewName: newName}); err != nil {\n    if strings.Contains(err.Error(), \"already exists\") {\n        // retry with a unique suffix\n    }\n    return fmt.Errorf(\"rename failed: %w\", err)\n}","preventionTips":["Generate unique names with a prefix+suffix scheme to avoid collisions","Cleanup target names in teardown scripts"],"tags":["docker","rename","container","daemon-error"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}