{"record":{"id":"c965d86be1e540bf","repo":"docker/cli","slug":"conflicting-options-cannot-specify-both-restart","errorCode":null,"errorMessage":"conflicting options: cannot specify both --restart and --rm","messagePattern":"conflicting options: cannot specify both --restart and --rm","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/container/opts.go","lineNumber":716,"sourceCode":"\t\tSecurityOpt:    securityOpts,\n\t\tStorageOpt:     storageOpts,\n\t\tReadonlyRootfs: copts.readonlyRootfs,\n\t\tLogConfig:      container.LogConfig{Type: copts.loggingDriver, Config: loggingOpts},\n\t\tVolumeDriver:   copts.volumeDriver,\n\t\tIsolation:      container.Isolation(copts.isolation),\n\t\tShmSize:        copts.shmSize.Value(),\n\t\tResources:      resources,\n\t\tTmpfs:          tmpfs,\n\t\tSysctls:        copts.sysctls.GetAll(),\n\t\tRuntime:        copts.runtime,\n\t\tMounts:         copts.mounts.Value(),\n\t\tMaskedPaths:    maskedPaths,\n\t\tReadonlyPaths:  readonlyPaths,\n\t\tAnnotations:    copts.annotations.GetAll(),\n\t}\n\n\tif copts.autoRemove && !hostConfig.RestartPolicy.IsNone() {\n\t\treturn nil, errors.New(\"conflicting options: cannot specify both --restart and --rm\")\n\t}\n\n\t// only set this value if the user provided the flag, else it should default to nil\n\tif flags.Changed(\"init\") {\n\t\thostConfig.Init = &copts.init\n\t}\n\n\t// When allocating stdin in attached mode, close stdin at client disconnect\n\tif config.OpenStdin && config.AttachStdin {\n\t\tconfig.StdinOnce = true\n\t}\n\n\tepCfg, err := parseNetworkOpts(copts)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn &containerConfig{","sourceCodeStart":698,"sourceCodeEnd":734,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/opts.go#L698-L734","documentation":"Returned when --rm (autoRemove) is enabled alongside a restart policy that is not \"no\" (opts.go:715-717). The default restart policy is RestartPolicyDisabled (\"no\"), so --rm alone is fine. The conflict arises because --rm deletes the container on exit while a restart policy (unless \"no\") would keep restarting it, making the two mutually exclusive. hostConfig.RestartPolicy.IsNone() is the discriminator.","triggerScenarios":"Running `docker run --rm --restart=always ...` or `--rm --restart=on-failure ...` or `--rm --restart=unless-stopped ...`. Any restart policy except \"no\" combined with --rm triggers it.","commonSituations":"Copy-pasting flags from a long-running template and adding --rm for cleanup; wrapping `docker run` in a retry script that also sets --restart; misreading --restart=no (allowed) vs other policies.","solutions":["Remove --rm if you want the container auto-restarted.","Use --restart=no (or omit --restart, since \"no\" is the default) if you want --rm behavior.","Switch to --restart=unless-stopped and manage lifecycle externally instead of --rm."],"exampleFix":"// before\ndocker run --rm --restart=always myimage\n// after (auto-remove, no restart)\ndocker run --rm myimage\n// after (auto-restart, no remove)\ndocker run --restart=always myimage","handlingStrategy":"validation","validationCode":"// Disallow --rm with any non-\"no\" restart policy up front.\nif copts.autoRemove && !container.RestartPolicyMode(copts.restartPolicy).IsNone() {\n    return errors.New(\"conflicting options: cannot specify both --restart and --rm\")\n}","typeGuard":"// isNoneRestart reports whether the policy is the \"no\" default.\nfunc isNoneRestart(policy string) bool {\n    return policy == \"\" || policy == \"no\"\n}","tryCatchPattern":null,"preventionTips":["Treat --rm and --restart (except no) as mutually exclusive in templates.","Default --restart to \"no\" so --rm composes cleanly.","Lint compose files for restart + auto_remove conflict."],"tags":["docker-cli","restart-policy","conflicting-options","validation","container-create"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}