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.
Error response from daemon: cannot stop container: 8f3a: Container 8f3a is already stoppedCommon 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
- Tolerate an already-stopped/absent container so teardown never fails the job.
docker stop myservice 2>/dev/null || true
docker rm myservice 2>/dev/null || trueCheck state before stopping
- Only stop the container if it is actually running.
if [ "$(docker inspect -f '{{.State.Running}}' myservice 2>/dev/null)" = "true" ]; then
docker stop myservice
fiHow to prevent it
- Guard cleanup commands with
|| trueor a running-state check. - Prefer
docker compose downwhich tolerates absent containers.