The '--restart and --rm' conflict error occurs when you use both flags together in a docker run command. These options are mutually exclusive because --rm removes the container on exit while --restart keeps it alive for restarts.
The "Conflicting options: --restart and --rm" error in Docker occurs when you attempt to use both the `--restart` and `--rm` flags together in the same `docker run` command. These two flags represent fundamentally incompatible container lifecycle behaviors: - **`--rm`**: Automatically removes the container and its writable layer when the container exits. This is ideal for ephemeral containers like one-off commands, build steps, or temporary development environments. - **`--restart`**: Configures a restart policy that tells the Docker daemon to automatically restart the container after it exits. Available policies include `no`, `on-failure`, `always`, and `unless-stopped`. The conflict is logical: you cannot tell Docker to both remove the container when it exits AND restart it when it exits. If Docker removed the container, there would be nothing left to restart. Conversely, if Docker plans to restart the container, it must preserve the container state. This error is a safeguard that prevents users from creating containers with contradictory configurations that would result in undefined behavior.
First, determine what behavior you actually need:
Use `--rm` when you want:
- One-off commands that run and clean up automatically
- Development/debugging containers that shouldn't persist
- Build steps or temporary processing containers
- Interactive shells you won't need to reconnect to
Use `--restart` when you want:
- Long-running services that should survive crashes
- Production containers that must stay running
- Background services that should start on boot
- Containers that might fail temporarily and need recovery
# Ephemeral container (use --rm)
docker run --rm alpine echo "Hello, World!"
# Persistent service (use --restart)
docker run -d --restart unless-stopped nginxOnce you've decided on the lifecycle behavior, remove the conflicting flag:
If you want automatic cleanup (remove --restart):
# Before (error)
docker run --rm --restart always myimage
# After (works)
docker run --rm myimageIf you want restart capability (remove --rm):
# Before (error)
docker run --rm --restart always myimage
# After (works)
docker run -d --restart always myimageNote: For detached containers (-d) that you want to restart, you almost always want --restart rather than --rm.
If using docker-compose run on a service with a restart policy, the --rm flag (which is the default for docker-compose run) conflicts with the service's restart policy.
Option 1: Override the restart policy for the run command:
# docker-compose.yml
services:
web:
image: myimage
restart: unless-stopped # This causes conflict with 'run --rm'# This might fail or produce unexpected behavior
docker-compose run --rm web echo "test"
# Use docker-compose exec instead for running commands in existing containers
docker-compose exec web echo "test"Option 2: Use docker-compose run without --rm:
# Don't use --rm (container persists)
docker-compose run web echo "test"
# Clean up manually afterward
docker-compose rm -f webOption 3: Create a separate service for one-off commands:
services:
web:
image: myimage
restart: unless-stopped
web-cli: # For one-off commands
image: myimage
profiles: ["cli"]
# No restart policy = compatible with --rmIf you have an existing container that was started with --rm and you want to add a restart policy, you cannot use docker update:
# This will fail for --rm containers
docker update --restart always mycontainer
# Error: Cannot update a container with AutoRemove enabledInstead, you must recreate the container:
# Stop and remove the existing container
docker stop mycontainer
# (it auto-removes due to --rm, or manually: docker rm mycontainer)
# Create a new container with the desired restart policy
docker run -d --name mycontainer --restart unless-stopped myimageFor containers without --rm, docker update works normally:
# Add restart policy to existing container
docker update --restart unless-stopped mycontainerA common pattern is using --rm in development but --restart in production. Use environment-specific scripts or compose files:
Using shell scripts:
#!/bin/bash
# run-dev.sh
docker run --rm -it -v $(pwd):/app myimage bash
# run-prod.sh
docker run -d --restart unless-stopped --name myapp myimageUsing Docker Compose override files:
# docker-compose.yml (base)
services:
app:
image: myimage
# docker-compose.override.yml (development - auto-loaded)
services:
app:
# No restart policy for development
# docker-compose.prod.yml (production)
services:
app:
restart: unless-stopped# Development (uses override automatically)
docker-compose up
# Production
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d### Restart Policy Options Explained
Docker provides four restart policies:
| Policy | Behavior |
|--------|----------|
| no | Never restart (default) |
| on-failure[:max] | Restart only on non-zero exit code, optionally with max retry count |
| always | Always restart regardless of exit code |
| unless-stopped | Like always, but don't restart if manually stopped |
Even --restart=no is technically a restart policy and will conflict with --rm, though Docker may handle this edge case differently in some versions.
### Why Docker Compose run Uses --rm by Default
The docker-compose run command is designed for one-off commands like running tests, migrations, or interactive shells. These are typically ephemeral by nature, so --rm makes sense as the default.
However, this conflicts with services that define restart policies for their long-running up behavior. The Docker Compose team addressed this in issue #3155, but the fundamental incompatibility remains.
### Alternative: Using Process Managers
If you need both automatic cleanup AND restart capability, consider using an external process manager:
# Using systemd (Linux)
[Unit]
Description=My Container
After=docker.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f mycontainer
ExecStart=/usr/bin/docker run --rm --name mycontainer myimage
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetThis approach:
- Uses --rm so Docker cleans up the container filesystem
- Uses systemd's Restart=always to restart the entire docker run command
- Gets fresh containers on each restart (no stale state)
### Container Orchestration Considerations
In Kubernetes or Docker Swarm, this conflict doesn't exist in the same way:
- Kubernetes Pods are always ephemeral (like --rm)
- Restart behavior is controlled by the restartPolicy field in the Pod spec
- The orchestrator handles creating new container instances
This separation of concerns is one reason why orchestrators are preferred for production workloads.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec