Error response from daemon: cannot remove container "...": container is running:
stop the container before removing or force remove means exactly what it says:
docker rm only removes stopped containers. Run docker stop
first, or use docker rm -f, which removes the running container without
waiting for a graceful stop.
The refusal protects whatever the container is doing: removing it would end its process and delete its files. Both ways out were tested below and both worked on the first try.
The error
PS> docker rm fix8-cli-run
Error response from daemon: cannot remove container "fix8-cli-run": container is running: stop the container before removing or force remove
Why it happens
A container is a process plus its filesystem layer and settings. docker rm
deletes the layer and the settings, and the engine will not do that while the process is
still alive. The container in the test was started with docker run -d ... sleep
infinity, so its main process never ends on its own, and docker rm
refused it.
This is common in scripts that clean up after a test run: the script removes containers
by name, but one of them is a long-running service that nobody stopped. It is also what
you meet when you re-run a docker run --name command and try to clear the
old container first.
The fix
Stop, then remove:
PS> docker stop fix8-cli-run
fix8-cli-run
PS> docker rm fix8-cli-run
fix8-cli-run
Or force the removal of a running container in one command:
PS> docker rm -f fix8-cli-run2
fix8-cli-run2
Stopping first is the careful option for anything that writes data, such as a database,
because you can still look at the stopped container before it goes.
docker rm -f is fine for throwaway containers. Each command prints the container name when it succeeds. For
containers that should vanish on exit, start them with docker run --rm and
there is nothing left to remove.
How it was reproduced
Docker Desktop on Windows 11, client and engine 29.9.2, Windows PowerShell 5.1, the
cached alpine:latest image. A container started with
docker run -d --name fix8-cli-run alpine sleep infinity gave the message on
docker rm; docker stop followed by docker rm
removed it, and docker rm -f removed a second running container directly.
The container names are the test's own.
Frequently asked
- How do I remove a running Docker container?
- Run docker stop <name> and then docker rm <name>, or docker rm -f <name> to remove it in one command without waiting for a graceful stop.
- What is the difference between docker stop and docker rm -f?
- docker stop gave a test container that traps SIGTERM the chance to run its handler (it logged got-TERM and exited with 0, after about 1.1 s) and left it on disk for docker rm. docker rm -f removed the same kind of container in about 0.4 s without waiting for a graceful stop.
- How do I stop Docker containers piling up after docker run?
- Start one-off containers with docker run --rm. Docker removes the container as soon as its main process exits.
More decoded errors in the Fixes category. If an old container is in the way of a new one with the same name, see container name already in use.