How to filter containers by a specific image?

Filtering Containers by a Specific Image

To filter containers by a specific Docker image, you can use the docker ps command with the appropriate options. The docker ps command allows you to list all the running containers on your system, and you can use various filters to narrow down the results.

Filtering by Image Name

The most straightforward way to filter containers by a specific image is to use the -f or --filter option with the docker ps command. The filter can be specified as image=<image_name>, where <image_name> is the name of the Docker image you want to filter by.

Here's an example:

docker ps -f image=nginx

This command will list all the running containers that are using the nginx image.

You can also use the --format option to customize the output and display only the specific information you need, such as the container ID, image name, and status.

docker ps -f image=nginx --format "{{.ID}} {{.Image}} {{.Status}}"

This will display the container ID, image name, and status for all the running containers that use the nginx image.

Filtering by Image Digest

In addition to filtering by the image name, you can also filter by the image digest. The image digest is a unique identifier for a specific image, and it can be useful when you want to ensure that you're working with a specific version of an image.

To filter by image digest, you can use the image=<image_name>@<digest> format in the filter.

docker ps -f image=nginx@sha256:d4ff5e66c8c5a8d9f5d2c2c3f9d4a7b0f5b7d7e7e6d5f4e3d2c1b0a9f8e7d6c5

This command will list all the running containers that are using the nginx image with the specified digest.

Filtering by Multiple Criteria

You can also combine multiple filters to narrow down the results even further. For example, you can filter by both the image name and the container status.

docker ps -f image=nginx -f status=running

This command will list all the running containers that are using the nginx image.

Visualizing the Filtering Process

Here's a Mermaid diagram that illustrates the process of filtering containers by a specific image:

graph TD A[docker ps] --> B{Filter by Image} B --> C[Filter by Image Name] B --> D[Filter by Image Digest] C --> E[Specify Image Name] D --> F[Specify Image Digest] E --> G[Display Filtered Containers] F --> G[Display Filtered Containers] G --> H[Customize Output]

In summary, to filter containers by a specific Docker image, you can use the docker ps command with the -f or --filter option, specifying the image name or digest as the filter criteria. You can also combine multiple filters to further refine the results and customize the output to display only the information you need.

0 Comments

no data
Be the first to share your comment!