Comprehensive Guide to Stopping Docker Containers by Image Name

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Docker | Container Management | Ubuntu System

Abstract: This technical article provides an in-depth exploration of various methods to stop running Docker containers based on image names in Ubuntu systems. Starting with Docker's native filtering capabilities for exact image tag matching, the paper progresses to sophisticated solutions for scenarios where only the base image name is known, including pattern matching using AWK commands. Through comprehensive code examples and step-by-step explanations, the guide offers practical operational procedures covering container stopping, removal, and batch processing scenarios for system administrators and developers.

Fundamentals of Docker Container Management

In Docker containerized environments, effective container lifecycle management represents a core responsibility for system operations. Ubuntu 14.04 (Trusty Tahr), as a widely deployed server operating system, presents particularly common requirements for Docker container management. When needing to stop running containers with only the image name used during container creation available, specific query and operation strategies must be employed.

Exact Image Tag Matching Solution

When the exact image tag (image:tag) is known, Docker provides native filtering mechanisms to locate relevant containers. This approach leverages the ancestor filter functionality introduced in Docker version 1.9, enabling precise matching of specific image versions.

The basic container query command is as follows:

docker ps -a -q --filter ancestor=<image-name>

This command returns a list of container IDs for all containers running from the specified image. The -a parameter ensures inclusion of containers in all states, while the -q parameter outputs only container IDs for subsequent processing.

In practical operations, querying is typically combined with stopping actions:

docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}")

For more comprehensive operations that include both stopping and container removal, command chaining can be employed:

docker rm $(docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}"))

Fuzzy Image Name Matching Strategy

In real-world operational scenarios, situations frequently arise where only the base image name is known without specific tag information. Since Docker's ancestor filter doesn't support wildcard matching, alternative approaches become necessary.

An effective solution combines docker ps with AWK commands for pattern matching. Convenience functions can be defined in ~/.bashrc:

dsi() { docker stop $(docker ps -a | awk -v i="^$1.*" '{if($2~i){print$1}}'); }

Usage example:

dsi alpine

This command stops all running containers whose image names begin with "alpine". The AWK pattern ^$1.* uses ^ to denote string beginning and .* to match any subsequent characters, with adjustment possible based on actual matching precision requirements.

For scenarios requiring both stopping and container removal, an extended function can be defined:

drmi() { docker rm $(dsi $1 | tr '\n' ' '); }

Usage pattern:

drmi alpine

Alternative Approach Comparison

Beyond the primary methods described, other viable alternatives exist. A simplified stopping command:

docker stop $(docker ps -q --filter ancestor=<image-name>)

This approach omits the -a parameter, operating exclusively on running containers, providing greater simplicity in specific scenarios.

Another strategy involves predefining container names during creation:

docker run -d --name <container-name> <image-name>

Subsequent management can then occur directly through container names:

docker stop <container-name>
docker rm <container-name>

This method offers more intuitive management in single-container scenarios but requires additional naming strategies in multi-container environments.

Technical Implementation Analysis

Docker's filtering mechanism relies on strict string matching, explaining why wildcards are unavailable in native filters. The ancestor filter requires complete image references, including repository, name, and tag components.

The AWK solution centers on parsing docker ps output. The second column ($2) of default docker ps -a output contains image information, with fuzzy querying achieved through regular expression matching.

In command chaining operations, tr '\n' ' ' converts newline characters to spaces, ensuring multiple container IDs are properly passed to the docker rm command. This processing becomes particularly important in scenarios involving container names containing spaces.

Production Environment Best Practices

In production environments, the following strategies are recommended: First validate matching container lists through query commands, confirming accuracy before executing stop operations. For business-critical containers, executing docker stop followed by waiting for normal termination before docker rm is advised to avoid data loss risks.

During batch operations, special attention should be paid to inter-container dependencies, ensuring stopping sequences don't compromise system stability. Regular cleanup of unused containers not only releases resources but also helps maintain system organization.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.