Skip to content
Latchkey

Docker "cannot stop container (already stopped)" in CI

A docker stop on a container that has already exited returns an error rather than a no-op. In CI this most often appears in a teardown step that assumes a service container is still running when it crashed or finished early.

What this error means

A cleanup step fails with Error response from daemon: cannot stop container: <id>: No such container or ... is not running, breaking the job even though the work succeeded.

docker
Error response from daemon: cannot stop container: 8f3a: Container 8f3a is already stopped

Common causes

The container already exited

A short-lived or crashed container is gone by the time the stop step runs.

A teardown step that assumes it is running

A hard-coded docker stop name in cleanup fails when the container is not up.

How to fix it

Make the stop idempotent

  1. Tolerate an already-stopped/absent container so teardown never fails the job.
Terminal
docker stop myservice 2>/dev/null || true
docker rm myservice 2>/dev/null || true

Check state before stopping

  1. Only stop the container if it is actually running.
Terminal
if [ "$(docker inspect -f '{{.State.Running}}' myservice 2>/dev/null)" = "true" ]; then
  docker stop myservice
fi

How to prevent it

  • Guard cleanup commands with || true or a running-state check.
  • Prefer docker compose down which tolerates absent containers.

Frequently asked questions

What causes ""cannot stop container""?
A short-lived or crashed container is gone by the time the stop step runs.
How do I fix "cannot stop container"?
Make the stop idempotent

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card