{"record":{"id":"644e8944f0133c3c","repo":"docker/cli","slug":"source-can-not-be-empty","errorCode":null,"errorMessage":"source can not be empty","messagePattern":"source can not be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/container/cp.go","lineNumber":141,"sourceCode":"\n// newCopyCommand creates a new `docker cp` command\nfunc newCopyCommand(dockerCLI command.Cli) *cobra.Command {\n\tvar opts copyOptions\n\n\tcmd := &cobra.Command{\n\t\tUse: `cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-\n\tdocker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH`,\n\t\tShort: \"Copy files/folders between a container and the local filesystem\",\n\t\tLong: `Copy files/folders between a container and the local filesystem\n\nUse '-' as the source to read a tar archive from stdin\nand extract it to a directory destination in a container.\nUse '-' as the destination to stream a tar archive of a\ncontainer source to stdout.`,\n\t\tArgs: cli.ExactArgs(2),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif args[0] == \"\" {\n\t\t\t\treturn errors.New(\"source can not be empty\")\n\t\t\t}\n\t\t\tif args[1] == \"\" {\n\t\t\t\treturn errors.New(\"destination can not be empty\")\n\t\t\t}\n\t\t\topts.source = args[0]\n\t\t\topts.destination = args[1]\n\t\t\tif !cmd.Flag(\"quiet\").Changed {\n\t\t\t\t// User did not specify \"quiet\" flag; suppress output if no terminal is attached\n\t\t\t\topts.quiet = !dockerCLI.Out().IsTerminal()\n\t\t\t}\n\t\t\treturn runCopy(cmd.Context(), dockerCLI, opts)\n\t\t},\n\t\tAnnotations: map[string]string{\n\t\t\t\"aliases\": \"docker container cp, docker cp\",\n\t\t},\n\t\tDisableFlagsInUseLine: true,\n\t}\n","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/cp.go#L123-L159","documentation":"In newCopyCommand RunE (cli/command/container/cp.go:139), after the Args validator (cli.ExactArgs(2)) ensures two args exist, the code explicitly checks args[0] at line 140-141 and returns errors.New(\"source can not be empty\") if the first positional arg is the empty string. An empty source path has no meaning for the copy operation.","triggerScenarios":"Running `docker cp \"\" dest`, `docker cp \"\" CONTAINER:/path`, or any invocation where the first positional argument resolves to an empty string (e.g. an unexpanded/empty shell variable like `docker cp \"$EMPTY\" web:/tmp`).","commonSituations":"Unset shell variables expanded to empty; quoting bugs; programmatic argv construction leaving args[0] as \"\"; `docker cp '' web:/x`.","solutions":["Provide a non-empty source path or container:path spec: `docker cp ./file web:/tmp`.","Quote-check your shell variables: ensure `$SRC` is set and non-empty before the command (e.g. `${SRC:?source required}`).","If streaming a tar via stdin, use '-' as the source, not the empty string."],"exampleFix":"# before\nSRC=\"\"\ndocker cp \"$SRC\" web:/tmp\n# after\nSRC=./file.txt\ndocker cp \"$SRC\" web:/tmp","handlingStrategy":"validation","validationCode":"// Guard shell variable expansion:\n// ${SRC:?source required} aborts if SRC is unset/empty.\n// In Go, before building argv:\nif source == \"\" { return errors.New(\"source can not be empty\") }","typeGuard":null,"tryCatchPattern":"if err := cmd.Execute(); err != nil {\n    if strings.Contains(err.Error(), \"source can not be empty\") {\n        // prompt for source / fix argv\n    }\n}","preventionTips":["Quote and check shell variables before `docker cp` (`${SRC:?}`).","Use '-' explicitly for stdin tar streaming, never the empty string.","Validate argv in wrappers before invoking docker."],"tags":["container","cp","validation","filesystem"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}