docker: conflicting options: cannot specify both --restart and --rm means one
docker run asked for a container that is deleted when it exits and also
restarted when it exits. Keep one: --rm for a one-off command,
--restart for a long-running service.
Drop --rm from a service that Docker should start again when it exits; drop
--restart from a command you run once and throw away. Both forms are shown
below, run and checked.
The error
docker run --rm --restart always alpine true
docker: conflicting options: cannot specify both --restart and --rm
Run 'docker run --help' for more information
The exit code was 125, and it was 125 in each of the three refused runs listed below. The
message starts with docker: and has no Error response from
daemon part, and its text was the same for all three refused policies.
Why it happens
The two options ask for opposite things when the main process exits:
--rm asks Docker to remove the container, and a restart policy asks Docker to
start it again. A --rm copied from a test command into a service's start
script is one way to end up with both on one line.
The test tried four policies with --rm. always,
on-failure and unless-stopped all gave the message;
--restart no, which sets no restart, was accepted and the container ran and
exited 0.
The fix
docker run --rm alpine true
docker run -d --name fix12-dkrun-rs --restart always alpine sleep 600
042a2868c608ed5db18f9cb68576d77ec6ad4b100d1de1b5980aa4cb151de46a
docker inspect -f "{{.HostConfig.RestartPolicy.Name}} {{.State.Status}}" fix12-dkrun-rs
always running
The first line is the one-off form: it ran and exited 0. The second is the service form:
a named, detached container with the always policy, and
docker inspect confirms the policy and that it is running. Nine minutes
later it was still listed as up, and the test removed it by hand together with another
test container:
docker ps -a --filter name=fix12-dkrun --format "{{.Names}} {{.Status}}"
fix12-dkrun-rs Up 9 minutes
fix12-dkrun-alp Up 9 minutes
docker rm -f fix12-dkrun-alp fix12-dkrun-rs
fix12-dkrun-alp
fix12-dkrun-rs
How it was reproduced
Docker Desktop 4.99.0 on Windows 11, client and engine 29.9.3, the cached
alpine:latest image (Alpine 3.24.2). A PowerShell script ran each command
through cmd /c with stderr merged into stdout, so the commands are shown
without a prompt. Runs: --rm with --restart always,
on-failure and unless-stopped refused, --rm --restart
no, --rm alone and --restart always alone accepted. The
container names fix12-dkrun-rs and fix12-dkrun-alp are the test's
own.
Frequently asked
- Can I use --rm and --restart together in docker run?
- In the test --restart always, on-failure and unless-stopped were each refused with --rm; --restart no with --rm was accepted because it sets no restart.
- Which should I use, --rm or --restart?
- --rm for a command you run once and throw away, --restart for a service that should come back. In the test a container started with --restart always was still up nine minutes later and had to be removed with docker rm -f.
- How do I check a container's restart policy?
- In the test docker inspect -f "{{.HostConfig.RestartPolicy.Name}} {{.State.Status}}" fix12-dkrun-rs printed always running.
More decoded errors in the Fixes category. For restart policies in a Compose file, see invalid restart policy.