Skip to content
DevOps AI ToolKit
Newsletter
All guides
Docker with AI By James Joyner IV · · 8 min read

Docker Error Guide: 'You cannot remove a running container' — Fix Container Removal Failures

Quick answer

Fix 'You cannot remove a running container' in Docker: stop the container first, use docker rm -f to force removal, and handle restart loops safely.

Part of the Docker Container & Runtime Errors hub
  • #docker
  • #troubleshooting
  • #errors
  • #containers
Free toolkit

Fixing errors like this? Get 500 free DevOps AI prompts

500 copy-paste AI prompts for the stack you actually run — one PDF, free.

Overview

Docker refuses to remove a container because it is still running:

Error response from daemon: You cannot remove a running container 3f9a1c2b7e04. Stop the container before attempting removal or force remove

docker rm deletes stopped containers by default to protect a live workload from being deleted out from under you. To remove a running container you must either stop it first or explicitly force removal.

Symptoms

  • docker rm myapp fails with You cannot remove a running container.
  • docker ps shows the container in the Up state.
  • A cleanup script errors when it tries to remove containers it did not stop.
  • The container is in a restart loop and keeps coming back before removal.

Common Root Causes

  • The container is running — the default docker rm will not delete an Up container.
  • A restart policy is respawning it--restart always/unless-stopped restarts it after a stop.
  • A cleanup script skips the stop step — it calls docker rm without a preceding docker stop.
  • The wrong container was targeted — a still-running container shares a similar name/ID.
  • An orchestrator is supervising it — Compose or systemd restarts the container you are trying to remove.

Diagnostic Workflow

Confirm the container’s state and restart policy:

docker ps --filter "name=myapp"
docker inspect --format '{{.State.Status}} restart={{.HostConfig.RestartPolicy.Name}}' myapp

The straightforward path — stop, then remove:

docker stop myapp
docker rm myapp

To do it in one step, force removal (this sends SIGKILL and removes):

docker rm -f myapp

If a restart policy keeps reviving it, clear the policy or stop the supervisor first:

docker update --restart no myapp
docker stop myapp
docker rm myapp

For containers managed by Compose, remove them through Compose so the supervisor does not restart them:

docker compose down

Example Root Cause Analysis

A nightly cleanup job failed to remove a service container:

You cannot remove a running container 3f9a1c2b7e04. Stop the container before attempting removal or force remove

docker inspect showed:

running restart=unless-stopped

The script ran docker stop myapp followed by docker rm myapp, but the container had --restart unless-stopped. Because docker stop counts as a manual stop, the policy should not have restarted it — yet a separate systemd unit was also managing the container and started it again between the stop and the rm. Two supervisors were fighting. The fix was to stop the systemd unit first, then remove the container, and consolidate on a single supervisor:

sudo systemctl stop myapp.service
docker rm -f myapp

The lesson: if removal keeps failing on a “stopped” container, something is restarting it — find and disable the supervisor before removing.

Prevention Best Practices

  • Always docker stop before docker rm, or use docker rm -f when you are certain the workload is disposable.
  • Let one system own each container’s lifecycle — Compose or systemd or manual, never two at once.
  • Clear restart policies (docker update --restart no) before removing long-lived containers.
  • Use docker compose down for Compose-managed stacks so the supervisor tears them down cleanly.
  • Use --rm for ephemeral containers so they self-remove on exit and never need manual cleanup.

Quick Command Reference

docker ps --filter "name=myapp"           # confirm it is running
docker inspect --format '{{.State.Status}}' myapp
docker stop myapp && docker rm myapp      # graceful stop then remove
docker rm -f myapp                        # force stop and remove
docker update --restart no myapp          # clear restart policy first
docker compose down                       # tear down Compose-managed containers

Conclusion

You cannot remove a running container is a safety guard: Docker will not delete a live container unless you stop it or pass -f. Stop first for a graceful shutdown, or force-remove disposable containers. If removal keeps failing, a restart policy or a second supervisor is reviving the container — disable that before removing. More runtime fixes are in the Docker stack guides.

Free download · 368-page PDF

Get 500 Battle-Tested DevOps AI Prompts — Free

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.