How to filter Docker containers by image?

Filtering Docker Containers by Image

Filtering Docker containers by image is a common task when working with Docker. This can be useful for various scenarios, such as managing your container environment, troubleshooting issues, or monitoring the health of your applications.

Listing Docker Containers by Image

The primary command to list Docker containers is docker ps. By default, this command will display all running containers. To filter the containers by their image, you can use the -f or --filter option.

Here's an example of how to list all containers running a specific image:

docker ps -f "ancestor=nginx:latest"

This command will display all containers that are using the nginx:latest image.

You can also use the --format option to customize the output and display only the information you need. For example, to show the container ID and the image name:

docker ps -f "ancestor=nginx:latest" --format "{{.ID}} {{.Image}}"

This will output something like:

a1b2c3d4e5f6 nginx:latest
g7h8i9j0k1l2 nginx:latest

Filtering by Multiple Criteria

You can also combine multiple filters to refine your search. For example, to list all stopped containers that are using the nginx:latest image:

docker ps -a -f "ancestor=nginx:latest" -f "status=exited"

The -a option includes both running and stopped containers in the output.

Filtering by Image Digest

In addition to filtering by image name, you can also filter by the image digest. This can be useful when you want to identify containers that are using a specific version of an image, identified by its unique digest.

To list containers by image digest, use the ancestor filter with the digest value:

docker ps -f "ancestor=nginx@sha256:abcd1234efgh5678ijkl9012mnop3456qrst"

Replace the digest value with the one you want to search for.

Visualizing the Filtering Process

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

graph TD A[Docker CLI] --> B[docker ps] B --> C[Filter by Image] C --> D[Filter by Multiple Criteria] C --> E[Filter by Image Digest] D --> F[Display Filtered Containers] E --> F

This diagram shows that the docker ps command is the starting point, and then you can apply various filters to narrow down the list of containers based on the image criteria you're interested in.

By using these filtering techniques, you can effectively manage and monitor your Docker containers, making it easier to maintain and troubleshoot your containerized applications.

0 Comments

no data
Be the first to share your comment!