docker/cli ยท error

conflicting options: cannot specify both --restart and --rm

Error message

conflicting options: cannot specify both --restart and --rm

What it means

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.

Source

Thrown at cli/command/container/opts.go:716

		SecurityOpt:    securityOpts,
		StorageOpt:     storageOpts,
		ReadonlyRootfs: copts.readonlyRootfs,
		LogConfig:      container.LogConfig{Type: copts.loggingDriver, Config: loggingOpts},
		VolumeDriver:   copts.volumeDriver,
		Isolation:      container.Isolation(copts.isolation),
		ShmSize:        copts.shmSize.Value(),
		Resources:      resources,
		Tmpfs:          tmpfs,
		Sysctls:        copts.sysctls.GetAll(),
		Runtime:        copts.runtime,
		Mounts:         copts.mounts.Value(),
		MaskedPaths:    maskedPaths,
		ReadonlyPaths:  readonlyPaths,
		Annotations:    copts.annotations.GetAll(),
	}

	if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() {
		return nil, errors.New("conflicting options: cannot specify both --restart and --rm")
	}

	// only set this value if the user provided the flag, else it should default to nil
	if flags.Changed("init") {
		hostConfig.Init = &copts.init
	}

	// When allocating stdin in attached mode, close stdin at client disconnect
	if config.OpenStdin && config.AttachStdin {
		config.StdinOnce = true
	}

	epCfg, err := parseNetworkOpts(copts)
	if err != nil {
		return nil, err
	}

	return &containerConfig{

View on GitHub (pinned to e9452d6e78)

Solutions

  1. Drop --rm if the container is a service that should survive exits via --restart
  2. Drop --restart (or use --restart=no) if this is a throwaway run that should clean up after itself
  3. For debug runs of a service, keep --rm and restart manually instead of via policy

Example fix

# before
docker run --rm --restart=always -d nginx
# after (service)
docker run --restart=always -d nginx
# or (one-off)
docker run --rm -d nginx

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of docker/cli@e9452d6e78 (2026-08-01). Data as JSON: /data/errors/c965d86be1e540bf.json. Report an issue: GitHub.