docker/cli · error

invalid argument: can't use stdin for both build context…

Error message

invalid argument: can't use stdin for both build context and dockerfile

What it means

runBuild() detects when the Dockerfile is to be read from stdin (dockerfileName == "-", i.e. `-f -`). If the build context is ALSO sourced from stdin (DetectContextType returns ContextTypeStdin, i.e. context path is `-`), the single stdin stream cannot serve both, so the CLI rejects the combination up front.

Solutions

  1. Put the Dockerfile inside the build context and pass a real context path
  2. Provide the context as a tarball on stdin and a real `-f <path>` Dockerfile
  3. Provide the Dockerfile on stdin (`-f -`) but give a directory path for the context

Example fix

// before
docker build -f - - < Dockerfile
// after
echo 'FROM scratch' > Dockerfile && docker build -f Dockerfile .
Defensive patterns

Strategy: validation

Validate before calling

dockerfileFromStdin := options.dockerfileName == "-"
contextType, _ := build.DetectContextType(options.context)
if dockerfileFromStdin && contextType == build.ContextTypeStdin {
    return errors.New("stdin cannot be used for both Dockerfile and build context")
}

Prevention

When it happens

Trigger: `docker build -f - -` (Dockerfile from stdin AND context from stdin), or piping only one stream while specifying `-` for both.

Common situations: Scripts that try to pipe a Dockerfile and a tarball simultaneously; confusion over which `-` means context vs Dockerfile.

Related errors


AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07). Data as JSON: /api/errors/33ef021411ec2cb3. Report an issue: GitHub.

Appendix: 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 4f84911bfe)