{"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/e9452d6e785f6e365712b9d71bd7517591773c86/cli/command/container/opts.go#L698-L734","documentation":"After parsing the restart policy into HostConfig, the CLI checks that --rm (AutoRemove) is not combined with any restart policy other than 'no' (opts.go:715-717). The two are semantically contradictory: --rm deletes the container when it exits, while --restart asks the daemon to restart it after exit, so the daemon could never both remove and restart it.","triggerScenarios":"`docker run --rm --restart=always image`, `--rm --restart=on-failure`, `--rm --restart=unless-stopped`, or `--rm --restart=on-failure:3` on run/create. Only `--restart=no` (the default, RestartPolicy.IsNone()) is allowed together with --rm.","commonSituations":"Adding --rm to a long-lived service command copied from docs that already had --restart; wrapper scripts/Makefiles that unconditionally append --rm for cleanup; converting a compose service (restart: always) to a docker run one-liner used for debugging.","solutions":["Drop --rm if the container is a service that should survive exits via --restart","Drop --restart (or use --restart=no) if this is a throwaway run that should clean up after itself","For debug runs of a service, keep --rm and restart manually instead of via policy"],"exampleFix":"# before\ndocker run --rm --restart=always -d nginx\n# after (service)\ndocker run --restart=always -d nginx\n# or (one-off)\ndocker run --rm -d nginx","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["docker-cli","restart-policy","auto-remove","conflicting-options"],"analyzedSha":"e9452d6e785f6e365712b9d71bd7517591773c86","analyzedAt":"2026-08-01T07:09:22.474Z","schemaVersion":2}