{"record":{"id":"10cb3eddcecbe48f","repo":"docker/cli","slug":"unable-to-prepare-context-from-stdin-w","errorCode":null,"errorMessage":"unable to prepare context from STDIN: %w","messagePattern":"unable to prepare context from STDIN: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/image/build.go","lineNumber":233,"sourceCode":"\tprogBuff = dockerCli.Out()\n\tbuildBuff = dockerCli.Out()\n\tif options.quiet {\n\t\tprogBuff = bytes.NewBuffer(nil)\n\t\tbuildBuff = bytes.NewBuffer(nil)\n\t}\n\tif options.imageIDFile != \"\" {\n\t\t// Avoid leaving a stale file if we eventually fail\n\t\tif err := os.Remove(options.imageIDFile); err != nil && !os.IsNotExist(err) {\n\t\t\treturn fmt.Errorf(\"removing image ID file: %w\", err)\n\t\t}\n\t}\n\n\tswitch contextType {\n\tcase build.ContextTypeStdin:\n\t\t// buildCtx is tar archive. if stdin was dockerfile then it is wrapped\n\t\tbuildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to prepare context from STDIN: %w\", err)\n\t\t}\n\tcase build.ContextTypeLocal:\n\t\tcontextDir, relDockerfile, err = build.GetContextFromLocalDir(options.context, options.dockerfileName)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to prepare context: %s\", err)\n\t\t}\n\t\tif strings.HasPrefix(relDockerfile, \"..\"+string(filepath.Separator)) {\n\t\t\t// Dockerfile is outside build-context; read the Dockerfile and pass it as dockerfileCtx\n\t\t\tdockerfileCtx, err = os.Open(options.dockerfileName)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to open Dockerfile: %w\", err)\n\t\t\t}\n\t\t\tdefer dockerfileCtx.Close()\n\t\t}\n\tcase build.ContextTypeGit:\n\t\tvar tempDir string\n\t\ttempDir, relDockerfile, err = build.GetContextFromGitURL(options.context, options.dockerfileName)\n\t\tif err != nil {","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/image/build.go#L215-L251","documentation":"Returned by runBuild (build.go:233) when build.GetContextFromRead fails while reading the build context from stdin (ContextTypeStdin). GetContextFromRead reads stdin, detects compression, and produces a tar archive plus the relative Dockerfile path. Failure means the stdin stream could not be parsed as a valid (optionally compressed) tar build context.","triggerScenarios":"Piping a non-tar/non-archive stream to 'docker build -' (e.g., 'cat Dockerfile | docker build -'), sending corrupted/truncated gzip data, or interrupting the stdin stream mid-transfer. Also fires when the stream is a tar but the internal Dockerfile path cannot be resolved.","commonSituations":"Confusing the build-context-from-stdin convention: users often think '-' reads a Dockerfile from stdin, but with a positional '-' the CLI expects a full tar context; piping raw Dockerfile text; network/SSH stream corruption; running 'docker build -' interactively and closing the pipe early.","solutions":["If you only have a Dockerfile, pass it via -f- with a separate context: 'docker build -f- . <<EOF ... EOF'.","If you intend a tar context, produce a valid tar: 'tar -czf - . | docker build -'.","Verify the stream is not truncated: pipe through 'tar -tzf -' first to confirm it is a readable archive.","Avoid piping non-archive content; use a local directory path as the context instead."],"exampleFix":"# before (wrong: raw Dockerfile text to stdin context)\ncat Dockerfile | docker build -\n# after (local dir context, Dockerfile from stdin)\ndocker build -f- . < Dockerfile\n# or: send a real tar archive\ntar -czf - . | docker build -","handlingStrategy":"validation","validationCode":"// Detect whether stdin is a usable (compressed) tar before building with '-'.\nfunc stdinLooksLikeTar(r io.Reader) bool {\n\tbr := bufio.NewReader(r)\n\tb, _ := br.Peek(2)\n\t// gzip magic 1f 8b or raw tar magic at offset 257 handled elsewhere\n\treturn len(b) >= 2 && b[0] == 0x1f && b[1] == 0x8b\n}","typeGuard":null,"tryCatchPattern":"if err := buildCmd.Execute(); err != nil {\n\tif strings.Contains(err.Error(), \"unable to prepare context from STDIN\") {\n\t\t// stdin was not a valid tar context; switch to a local dir or -f- for Dockerfile-only\n\t}\n}","preventionTips":["Use '-' only when piping a real tar (or tar.gz) archive.","For Dockerfile-only input, use 'docker build -f- <dir>'.","Validate the stream with 'tar -tzf -' before piping to docker build."],"tags":["build","stdin","tar","context"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}