docker ps -a --filter is a very useful command for filtering the list of Docker containers based on specific conditions. It provides a powerful capability to quickly find the desired container in environments with many containers.
Below, I will explain the main filter options along with actual use cases.
Basic Format
docker ps -a --filter <filter_name>=<filter_value>
You can use multiple filters at the same time by employing several --filter options.
🔍 Key Filter Options and Use Cases
1. Filtering by Status
This is used to filter by the state of the containers.
| Status Option | Description |
|---|---|
| created | Containers that have been created but not yet started |
| restarting | Containers that are continuously restarting |
| running | Containers that are currently running |
| paused | Paused containers |
| exited | Terminated containers |
Example: Show only running containers
docker ps -a --filter status=running
Example: Show only exited containers
docker ps -a --filter status=exited
2. Filtering by Container Name
This is useful for finding containers that contain a specific name.
Example: Show only containers with "web" in the name
docker ps -a --filter name=web
- Example Result:
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES f1c2d3e4f5 nginx:latest "/start" 2 days ago Up 2 days my-web-container
3. Filtering by Image
This is useful for easily finding containers that use a specific image.
Example: Show containers using the nginx image
docker ps -a --filter ancestor=nginx
- Example Result:
CONTAINER ID IMAGE COMMAND STATUS NAMES
a1b2c3d4e5 nginx:latest "/start" Up 3 hours nginx-server-1
f6g7h8i9j0 nginx:alpine "/start" Exited nginx-temp
4. Filtering by Volume
This is useful for finding containers that have a specific volume mounted.
Example: Show containers using the my_volume volume
docker ps -a --filter volume=my_volume
- Example Result:
CONTAINER ID IMAGE COMMAND STATUS NAMES
1a2b3c4d5e postgres "docker-entry" Up 2 days postgres_db
5. Filtering by Label
This is useful for managing containers with labels in Docker Compose or Kubernetes.
Example: Show containers with the label "env=production"
docker ps -a --filter label=env=production
- Example Result:
CONTAINER ID IMAGE COMMAND STATUS NAMES
abcdef12345 myapp:latest "/run-app" Up 3 days myapp-production
6. Filtering by Network
This is useful for checking the list of containers connected to a specific network.
Example: Show containers connected to a specific network
docker ps -a --filter network=my_network
- Example Result:
CONTAINER ID IMAGE COMMAND STATUS NAMES
123abc456de redis "/start" Up 1 day redis-cache
456def789gh postgres "/start" Up 1 day postgres-db
7. Filtering by ID
This is used to quickly search for a specific container ID (usually you only need to enter part of the ID).
Example: Show containers starting with ID 1234
docker ps -a --filter id=1234
💡 Using Multiple Filters
Multiple filters can be combined.
Example: Find containers that contain "web" in the name, are "running", and use the "nginx" image
docker ps -a --filter name=web --filter status=running --filter ancestor=nginx
🛠️ Practical Use Cases
- To stop all containers of a specific project:
docker stop $(docker ps -a -q --filter name=my_project)****
- To remove old exited containers:
docker rm $(docker ps -a -q --filter status=exited)
- To clean up unnecessary temporary containers:
docker rm $(docker ps -a -q --filter ancestor=temp_image)
Thus, the docker ps -a --filter command is very practical and powerful for quickly filtering and managing when there are many containers. Make active use of it!
There are no comments.