Docker Error Guide: 'Cannot kill container: is not running' — Already-Stopped Container
Fix 'Cannot kill container: Container is not running': the target already exited. Learn to check state with docker ps, use rm/start correctly, and script idempotent stops.
- #docker
- #troubleshooting
- #errors
- #daemon
Overview
You try to stop or kill a container and the daemon says there is nothing running to kill:
Error response from daemon: Cannot kill container 3f9ac1b2e7d4: Container 3f9ac1b2e7d4 is not running
docker kill (and the SIGKILL/SIGTERM path used by docker stop) only applies to a running container. If the container has already exited — because it finished, crashed, or was stopped a moment ago — there is no live process to signal, so the daemon refuses. This is usually a harmless race or a stale reference, not a broken container.
Symptoms
docker kill <id>ordocker stop <id>returnsContainer ... is not running.docker psdoes not list the container, butdocker ps -ashows it inExitedstate.- A teardown script fails on the kill step even though the container is already gone/stopped.
- Two processes (a script and a health monitor) both try to stop the same container; the second loses the race.
- The container has
restart: noand exited on its own after completing its task.
Common Root Causes
- The container already exited. A short-lived or one-shot container finished before the kill command ran.
- It was already stopped. A prior
docker stop/docker killsucceeded; a second call has nothing to do. - A crash on startup. The process died immediately, so the container was never really “running” by the time you signaled it.
- Race conditions in scripts. Parallel teardown steps or a monitor and a deploy job both target the same container.
- Stale ID/name reference. You are killing an old container ID that was replaced by a redeploy.
Diagnostic Workflow
Check the live state first — running vs exited is the whole story:
docker ps # running containers only
docker ps -a # includes exited/stopped containers
Look at exactly why and when it stopped:
docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}} started={{.State.StartedAt}} finished={{.State.FinishedAt}}' 3f9ac1b2e7d4
docker logs 3f9ac1b2e7d4
If the container is stopped and you simply want it gone, remove it (idempotently) rather than killing it:
docker rm -f 3f9ac1b2e7d4 # force-removes whether running or stopped
If you actually need it running again:
docker start 3f9ac1b2e7d4
docker ps
Example Root Cause Analysis
A CI job ran a database migration in a throwaway container, then a cleanup step killed it:
docker run -d --name migrate registry.example.com/myapp:1.4.2 ./run-migrations.sh
# ... later ...
docker kill migrate
Intermittently the pipeline failed with Cannot kill container migrate: Container migrate is not running. The migration container was designed to run to completion and exit on its own. On fast runners it finished before the cleanup step reached the docker kill line, so by the time the kill ran, the container had already exited — nothing to signal.
docker ps -a confirmed the container in Exited (0) state with a normal exit code, proving it had completed successfully, not crashed. The fix was to replace docker kill migrate with docker rm -f migrate, which removes the container regardless of whether it is running or already stopped, making the cleanup idempotent. The pipeline stopped flaking because the teardown no longer assumed the container was still alive.
Prevention Best Practices
- Prefer
docker rm -ffor teardown. It removes running or stopped containers alike, so cleanup never assumes a live process. - Check state before signaling in scripts: gate
docker killondocker ps -q --filter name=<name>returning an ID. - Ignore the benign failure where appropriate: append
|| trueto kill/stop in teardown so an already-exited container does not fail the job. - Serialize teardown so a monitor and a deploy job do not race to stop the same container.
- Use
--rmfor one-shot containers so they clean themselves up on exit and leave no stale reference to kill. See the Docker stack guides.
Quick Command Reference
# See running vs exited state
docker ps
docker ps -a
# Why/when did it stop?
docker inspect --format '{{.State.Status}} {{.State.ExitCode}}' <id>
docker logs <id>
# Idempotent teardown (running or stopped)
docker rm -f <id>
# Restart if you needed it running
docker start <id>
Conclusion
Cannot kill container: Container is not running simply means the target already exited before your signal arrived. Check docker ps -a to confirm its state, inspect the exit code to rule out a crash, and switch teardown scripts to docker rm -f (or guard the kill with a state check and || true). Treating stops as idempotent removes the race that produces this error.
Download the Free 500-Prompt DevOps AI Toolkit
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.