{"record":{"id":"fe47b92bf94898f1","repo":"docker/cli","slug":"conflicting-options-cannot-specify-both-attach","errorCode":null,"errorMessage":"conflicting options: cannot specify both --attach and --detach","messagePattern":"conflicting options: cannot specify both --attach and --detach","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/container/run.go","lineNumber":134,"sourceCode":"\t}\n\treturn runContainer(ctx, dockerCLI, ropts, copts, containerCfg)\n}\n\n//nolint:gocyclo\nfunc runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOptions, copts *containerOptions, containerCfg *containerConfig) error {\n\tconfig := containerCfg.Config\n\tstdout, stderr := dockerCli.Out(), dockerCli.Err()\n\tapiClient := dockerCli.Client()\n\n\tconfig.ArgsEscaped = false\n\n\tif !runOpts.detach {\n\t\tif err := dockerCli.In().CheckTty(config.AttachStdin, config.Tty); err != nil {\n\t\t\treturn err\n\t\t}\n\t} else {\n\t\tif copts.attach.Len() != 0 {\n\t\t\treturn errors.New(\"conflicting options: cannot specify both --attach and --detach\")\n\t\t}\n\n\t\tconfig.AttachStdin = false\n\t\tconfig.AttachStdout = false\n\t\tconfig.AttachStderr = false\n\t\tconfig.StdinOnce = false\n\t}\n\n\tdetachKeys := runOpts.detachKeys\n\tif detachKeys == \"\" {\n\t\tdetachKeys = dockerCli.ConfigFile().DetachKeys\n\t}\n\tif err := validateDetachKeys(runOpts.detachKeys); err != nil {\n\t\treturn err\n\t}\n\n\tcontainerID, err := createContainer(ctx, dockerCli, containerCfg, &runOpts.createOptions)\n\tif err != nil {","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/run.go#L116-L152","documentation":"Returned by runContainer when --detach is set but copts.attach still has entries (--attach / -a). Attaching to streams is meaningless when detached, so the CLI rejects the combination before creating the container. The guard is at run.go:132-135 inside the detach branch.","triggerScenarios":"`docker run -d --attach STDOUT alpine` or `docker run --detach --attach STDIN ...`. detach is true and copts.attach.Len() != 0.","commonSituations":"Migrating an attached-run command to backgrounded and forgetting to remove --attach. Aliases/scripts that always add -a. Confusion between --attach (stream attach) and -i/-a for interactive.","solutions":["Choose one: drop --attach to run detached, or drop --detach to attach.","Use -d alone for background containers; the daemon streams are not consumed by your terminal.","If you need logs from a detached container, use `docker logs` afterward."],"exampleFix":"// before\ndocker run -d --attach STDOUT alpine\n\n// after\ndocker run -d alpine","handlingStrategy":"validation","validationCode":"if detach && len(attachStreams) > 0 {\n    return errors.New(\"conflicting options: cannot specify both --attach and --detach\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide run mode upfront: attached (interactive) vs detached (backgrounded).","In scripts, make --attach and --detach mutually exclusive via your flag builder.","Use `docker logs` for detached container output instead of --attach."],"tags":["cli-options","conflicting-flags","container","run"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}