{"record":{"id":"ae64a524d4684d3b","repo":"docker/cli","slug":"error-reading-from-stdin-data-is-empty","errorCode":null,"errorMessage":"error reading from STDIN: data is empty","messagePattern":"error reading from STDIN: data is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/config/create.go","lineNumber":118,"sourceCode":"// as defined by [MaxConfigSize] in SwarmKit.\n//\n// [MaxConfigSize]: https://pkg.go.dev/github.com/moby/swarmkit/v2@v2.0.0-20250103191802-8c1959736554/manager/controlapi#MaxConfigSize\nconst maxConfigSize = 1000 * 1024 // 1000KB\n\n// readConfigData reads the config from either stdin or the given fileName.\n//\n// It reads up to twice the maximum size of the config ([maxConfigSize]),\n// just in case swarm's limit changes; this is only a safeguard to prevent\n// reading arbitrary files into memory.\nfunc readConfigData(in io.Reader, fileName string) ([]byte, error) {\n\tswitch fileName {\n\tcase \"-\":\n\t\tdata, err := io.ReadAll(io.LimitReader(in, 2*maxConfigSize))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error reading from STDIN: %w\", err)\n\t\t}\n\t\tif len(data) == 0 {\n\t\t\treturn nil, errors.New(\"error reading from STDIN: data is empty\")\n\t\t}\n\t\treturn data, nil\n\tcase \"\":\n\t\treturn nil, errors.New(\"config file is required\")\n\tdefault:\n\t\t// Open file with [FILE_FLAG_SEQUENTIAL_SCAN] on Windows, which\n\t\t// prevents Windows from aggressively caching it. We expect this\n\t\t// file to be only read once. Given that this is expected to be\n\t\t// a small file, this may not be a significant optimization, so\n\t\t// we could choose to omit this, and use a regular [os.Open].\n\t\t//\n\t\t// [FILE_FLAG_SEQUENTIAL_SCAN]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#FILE_FLAG_SEQUENTIAL_SCAN\n\t\tf, err := sequential.Open(fileName)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error reading from %s: %w\", fileName, err)\n\t\t}\n\t\tdefer f.Close()\n\t\tdata, err := io.ReadAll(io.LimitReader(f, 2*maxConfigSize))","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/config/create.go#L100-L136","documentation":"readConfigData (cli/command/config/create.go:110) handles the '-' (stdin) case for `docker config create NAME -`. It reads up to 2*maxConfigSize bytes from stdin; if the resulting data has length 0 it returns errors.New(\"error reading from STDIN: data is empty\") at line 118. An empty Swarm config is invalid, so the input must contain at least one byte.","triggerScenarios":"Running `docker config create mycfg -` with stdin closed, redirected from /dev/null, or piped from a command that produced no output (e.g. `echo -n '' | docker config create mycfg -`, or `< /dev/null`, or a here-doc with no content).","commonSituations":"Piping `cat` of an empty file; a preceding command in a pipeline that failed silently and emitted nothing; CI with stdin not attached.","solutions":["Ensure the piped/redirected stdin source actually contains data: `cat nonemptyfile | docker config create mycfg -`.","Check the preceding pipeline step did not fail or emit empty output (`set -o pipefail`, inspect `$?`).","If you have a file, pass it directly instead of '-': `docker config create mycfg ./config.txt`.","Verify the file/source is not /dev/null or an empty file."],"exampleFix":"# before\necho -n '' | docker config create mycfg -\n# after\nprintf 'my config contents' | docker config create mycfg -","handlingStrategy":"validation","validationCode":"// If streaming via stdin, check readability/non-emptiness upstream:\nfi, _ := os.Stdin.Stat()\nif (fi.Mode()&os.ModeCharDevice) != 0 || fi.Size() == 0 {\n    return errors.New(\"stdin is empty; provide config data or a file path\")\n}","typeGuard":null,"tryCatchPattern":"data, err := readConfigData(in, file)\nif err != nil {\n    if errors.Is(err, errEmptyStdin) /* if exposed */ || strings.Contains(err.Error(), \"data is empty\") {\n        // guide user to provide non-empty stdin\n    }\n    return err\n}","preventionTips":["Verify your pipeline produces output before piping to `docker config create NAME -`.","Use `set -o pipefail` and check $? to catch upstream failures.","Pass a file path directly when you have one."],"tags":["config","swarm","stdin","validation"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}