Docker
Container
Error
Troubleshooting
Image

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.

Practice system design

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.

bash
docker images

Then list all containers using that image.

bash
docker ps -a --filter ancestor=my-app:latest

If you only have the image ID:

bash
docker ps -a --filter ancestor=sha256:3f4b9c...

For detailed metadata, inspect containers and confirm the linked image.

bash
docker inspect <container_id> --format '{{.Image}} {{.Name}} {{.State.Status}}'

This gives a safe mapping before you remove anything.

Safe Cleanup Sequence

Use a staged cleanup instead of -f by default.

  1. Stop container if it is running.
bash
docker stop <container_id>
  1. Remove the stopped container.
bash
docker rm <container_id>
  1. Remove the image after no containers reference it.
bash
docker rmi my-app:latest

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.

bash
docker rmi -f my-app:latest

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 stop and rm for known containers,
  • use -f only 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:

bash
docker container prune

Remove dangling images:

bash
docker image prune

Remove all unused images, not only dangling ones:

bash
docker image prune -a

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.

  1. Find referencing containers.
bash
docker ps -a --filter ancestor=backend:dev
  1. Inspect one container to verify status.
bash
docker inspect a1b2c3 --format '{{.Name}} {{.State.Status}}'
  1. Remove old containers.
bash
docker rm a1b2c3 d4e5f6
  1. Retry image removal.
bash
docker rmi backend:dev
  1. Confirm cleanup.
bash
docker images | grep backend

This sequence gives deterministic results and minimizes accidental deletions.

Compose and Kubernetes Notes

With Docker Compose, stopped service containers remain until explicit cleanup.

bash
docker compose down

To also remove images built by Compose:

bash
docker compose down --rmi local

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 -a and --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 down is usually part of the correct cleanup path.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.