Stopping Docker containers by image name - Ubuntu
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want to stop Docker containers by image name, you first need to identify which running containers were created from that image and then pass their container IDs to docker stop. On Ubuntu, the shell makes this easy with Docker's filtering options plus standard command-line piping.
Find containers by image ancestry
Docker can filter running containers using the ancestor filter, which matches containers created from a given image.
That shows all running containers based on the nginx image. To return only their container IDs, use -q.
Those IDs are what docker stop needs.
Stop them directly
The shortest pattern is:
This does three things:
- lists matching running container IDs
- passes those IDs to
xargs - calls
docker stopon each one
The -r flag on Ubuntu's xargs avoids running docker stop when there are no matching containers.
Example with a specific tag
If you need to target a tagged image exactly, include the tag.
This is safer than matching only the repository name when multiple versions may be running.
Preview before stopping anything
If you are operating on production or shared development machines, preview the matches first.
That gives you a human-readable confirmation before you stop anything.
A reusable shell function
If you do this often, a small shell function is clearer than copying pipelines repeatedly.
This is easier to remember and reduces quoting mistakes.
Compose projects are different
If those containers were started by Docker Compose, it is often better to manage them through Compose itself.
Compose understands the project as a unit, while image-based stopping only looks at ancestry.
Be explicit about what you match
When several repositories share a common base image, stopping by image ancestry can affect more containers than expected. A quick preview with docker ps --filter "ancestor=..." is the safest "dry run" before you stop anything on a shared machine.
Force versus graceful stop
docker stop sends a termination signal and then waits before killing the process if it does not exit. That is usually what you want. If you use docker kill instead, containers do not get the same chance to shut down cleanly.
That distinction matters for databases, queues, and other stateful services.
If you need a different grace period, Docker lets you set one explicitly. That can help when containers need a few extra seconds to flush logs or finish in-flight work.
Common Pitfalls
A common mistake is filtering by a partial image name and accidentally matching more containers than intended. Tags matter when several image versions coexist.
Another issue is forgetting that ancestor matches containers created from that image, not arbitrary metadata you may have attached later.
It is also easy to use xargs without -r and get noisy or confusing behavior when no matching containers are running.
Summary
- Use
docker ps --filter "ancestor=..."to find containers created from a given image. - Add
-qto get only container IDs. - Pipe the IDs into
xargs -r docker stopto stop them cleanly on Ubuntu. - Preview the matches first when accuracy matters.
- Prefer
docker compose stopwhen the containers are part of a Compose-managed application.
Related reading
- String operation on env variables on Kubernetes
- sudo docker-machine command not found
- Tail docker logs to see recent records, not all
- Tainting in k8s
- TCP ingress support in Kubernetes
- Tensor flow serving docker invalid field
- TensorFlow in nvidia-docker failed call to cuInit CUDA_ERROR_UNKNOWN
- Tensorflow not found on pip install inside Docker Container using Mac M1

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.