{"record":{"id":"4d4021efffda0fb5","repo":"docker/cli","slug":"error-reading-from-stdin-w","errorCode":null,"errorMessage":"error reading from STDIN: %w","messagePattern":"error reading from STDIN: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/config/create.go","lineNumber":115,"sourceCode":"}\n\n// maxConfigSize is the maximum byte length of the [swarm.ConfigSpec.Data] field,\n// 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)","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/config/create.go#L97-L133","documentation":"Returned by readConfigData() when stdin ('-') is the source for `docker config create` and io.ReadAll on the limited stdin reader returns an error (cli/command/config/create.go:113-116). The reader is wrapped with io.LimitReader(in, 2*maxConfigSize); an I/O error during the read (not emptiness, which is a separate check) produces this wrapped error.","triggerScenarios":"Running `docker config create NAME -` and piping/typing input where the read itself fails: a broken pipe, premature close of stdin, or a terminal EOF error. Note: empty stdin yields a separate 'data is empty' error, not this one.","commonSituations":"Pipe source exits/crashes mid-stream (SIGPIPE), redirecting from a file descriptor that is closed, or running interactively without supplying input correctly. Input larger than 2*maxConfigSize (2 MB) is silently truncated, not errored, so the error here is specifically an OS-level read failure.","solutions":["Ensure the piped source completes successfully before stdin closes (check the producer's exit code).","Avoid piping from a command that may fail or be interrupted mid-stream.","If the data is on disk, pass the file path directly instead of '-'.","Verify the data is under 2*maxConfigSize to avoid truncation-related surprises."],"exampleFix":"# before\ngenerate-config | docker config create mycfg -   # generate-config crashes mid-pipe\n# after\nset -o pipefail\ngenerate-config > /tmp/cfg.json && docker config create mycfg /tmp/cfg.json","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// If you script `docker config create NAME -`, capture the producer exit and retry.\nif ! produce | docker config create mycfg -; then\n    if [ ${PIPESTATUS[0]} -ne 0 ]; then echo \"producer failed\"; fi\nfi","preventionTips":["Use set -o pipefail so a crashing producer is visible.","Prefer writing to a temp file and creating from it when the producer is unreliable."],"tags":["config","swarm","stdin","io"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}