image is being used by stopped container error
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The Docker error saying an image is used by a stopped container appears when you try to remove an image that still has container references. The container does not need to be running for this lock to exist. To fix it safely, identify the referencing containers first, then remove only what you intend to delete.
Why Docker Blocks Image Removal
An image is a read-only template. A container is an instance created from that template, and Docker keeps metadata linking the container to its source image. Even after docker stop, the container still exists, so the image stays in use.
This is why docker rmi IMAGE_ID can fail even when docker ps shows nothing active. docker ps lists running containers only. You need docker ps -a to include stopped ones.
Find Exactly Which Containers Reference the Image
Start with the image identifier.
Then list all containers using that image.
If you only have the image ID:
For detailed metadata, inspect containers and confirm the linked image.
This gives a safe mapping before you remove anything.
Safe Cleanup Sequence
Use a staged cleanup instead of -f by default.
- Stop container if it is running.
- Remove the stopped container.
- Remove the image after no containers reference it.
This path avoids deleting objects unexpectedly and keeps troubleshooting clearer.
When docker rmi -f Is Appropriate
Force removal is useful in local dev when you know references are disposable.
Even then, use it intentionally. Force actions can break repeatable debug flows, especially if teammates rely on the same local artifacts.
A practical policy is:
- prefer explicit
stopandrmfor known containers, - use
-fonly for short-lived local environments, - avoid force deletion on shared hosts.
Pruning Unused Objects to Prevent Recurrence
Frequent experiment cycles leave many stopped containers and dangling images. Controlled pruning keeps your machine clean.
Remove stopped containers:
Remove dangling images:
Remove all unused images, not only dangling ones:
If you run this in CI or shared environments, make sure retention expectations are documented first.
Example End-to-End Troubleshooting Session
Suppose docker rmi backend:dev fails with an image-in-use message.
- Find referencing containers.
- Inspect one container to verify status.
- Remove old containers.
- Retry image removal.
- Confirm cleanup.
This sequence gives deterministic results and minimizes accidental deletions.
Compose and Kubernetes Notes
With Docker Compose, stopped service containers remain until explicit cleanup.
To also remove images built by Compose:
In Kubernetes local setups, deletion may fail because local build containers are still referenced by helper processes. Check for leftover containers created by tools such as local build scripts or test harnesses.
Common Pitfalls
The most common mistake is checking only running containers with docker ps. Stopped containers still block image deletion.
Another mistake is force deleting images repeatedly without understanding the owner containers. That hides root cause and creates unstable local environments.
Developers also overuse broad prune commands on shared machines. That can remove artifacts other workflows still need. In shared contexts, scope cleanup to specific projects.
A smaller but common issue is tag confusion. You may remove one tag while another tag still points to the same image ID. Validate by ID when troubleshooting.
Summary
- Docker blocks image removal while any container metadata still references that image.
- Use
docker ps -aand--filter ancestor=to find blockers quickly. - Prefer explicit stop and remove steps before force deletion.
- Use prune commands carefully and with environment awareness.
- In Compose workflows,
docker compose downis usually part of the correct cleanup path.
Related reading
- ImagePullBackOff local repository with Minikube
- ImagePullBackOff unauthorized authentication required
- ImagePullSecrets GCR
- Import broker definitions into Dockerized RabbitMQ
- Implementing Gradient Descent In Python and receiving an overflow error
- Import error No module name urllib2
- Import data.sql MySQL Docker Container
- In a Dockerfile, How to update PATH environment variable?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.