docker/cli ยท error
invalid argument: can't use stdin for both build context and
Error message
invalid argument: can't use stdin for both build context and dockerfile
What it means
docker build reads the build context and optionally the Dockerfile from stdin, but stdin is a single stream and cannot serve both. This check fires when the Dockerfile flag is '-' (options.dockerfileFromStdin()) while the build context argument was also detected as stdin (build.ContextTypeStdin, i.e. the PATH argument is '-'). Allowing both would make the CLI consume the same stream twice, so it is rejected up front as an invalid argument.
Source
Thrown at cli/command/image/build.go:210
buildBuff io.Writer
remote string
)
if options.platform != "" {
_, err := platforms.Parse(options.platform)
if err != nil {
return err
}
}
contextType, err := build.DetectContextType(options.context)
if err != nil {
return err
}
if options.dockerfileFromStdin() {
if contextType == build.ContextTypeStdin {
return errors.New("invalid argument: can't use stdin for both build context and dockerfile")
}
dockerfileCtx = dockerCli.In()
}
progBuff = dockerCli.Out()
buildBuff = dockerCli.Out()
if options.quiet {
progBuff = bytes.NewBuffer(nil)
buildBuff = bytes.NewBuffer(nil)
}
if options.imageIDFile != "" {
// Avoid leaving a stale file if we eventually fail
if err := os.Remove(options.imageIDFile); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("removing image ID file: %w", err)
}
}
switch contextType {View on GitHub (pinned to e9452d6e78)
Solutions
- Pick one stdin use: pipe only the Dockerfile with a directory context: `docker build -f - . < Dockerfile`.
- Or pipe only the context and include the Dockerfile inside it: `tar c . | docker build -` (the tar must contain the Dockerfile).
- If the Dockerfile is generated, write it to a temp file and pass `-f /tmp/Dockerfile.gen .` instead of '-'.
Example fix
# before cat Dockerfile | docker build -f - - # after cat Dockerfile | docker build -f - .
When it happens
Trigger: `docker build -f - -` or equivalents: build context PATH given as '-' combined with `--file -` (or -f -). Also hit when a script pipes a tar context on stdin and separately sets DOCKERFILE input to '-'.
Common situations: CI pipelines that pipe a tarred context (`tar c . | docker build -`) while a shared wrapper adds `-f -` to inject a generated Dockerfile; copy-pasted one-liners like `cat Dockerfile | docker build -f - -`; Makefiles composing flags from variables where both default to '-'.
Related errors
- error reading from STDIN: data is empty
- content size not available for stdin
- requested load from stdin, but stdin is empty
- the --password-stdin option requires --username to be set
- error reading from STDIN: data is empty
AI-assisted analysis of docker/cli@e9452d6e78 (2026-08-01).
Data as JSON: /data/errors/33ef021411ec2cb3.json.
Report an issue: GitHub.