Docker Container ls Filter: 13 Ultimate Hacks!

Managing Docker containers can be troublesome use a Docker container ls filter with the docker container ls command to filter the output and manage effectively.

Kashyap Merai / / docker  · 8 min read

Managing Docker containers can be troublesome use a Docker container ls filter with the docker container ls command to filter the output and manage effectively.

Introduction: Docker Container ls Filter

Managing many Docker containers can be troublesome especially when the default output for the Docker container: docker container ls gives extensive output making it difficult to find the specific container.

You can use a Docker container ls filter with the docker container ls command to filter the output to improve efficiency while working with the container. You can use a filter to narrow the list of containers displayed in output with various conditions.

Benefits of using Docker container ls filters:

  • Reduced clutter: Filter out only relevant information, and focus on what matters the most
  • Efficiency: Quickly find specific containers without searching a long list
  • Quick Troubleshoot: Isolate and debug the container that meets the search criteria

By learning the Docker container ls filter effectively, you can troubleshoot and control the Docker container efficiently.

Docker-Defence-EBook

Docker Security Checklist + Guide

Practical, proven strategies to secure your containers like a pro - in under 20 minutes.

Mastering Docker Container ls Filter

Basic: Docker Container ls Filter

The docker container ls the command provides the basic overview of your running containers:

docker container ls [OPTIONS]

[OPTIONS] part you can specify the additional powerful options. Let’s check on how to list all the containers, running and exited:

docker container ls -a

-a flag informs docker container ls to display all containers with running, exited, etc status.

Example Output:

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
a3d5e6f4b1a7   python     "python app.py"          2 days ago     Exited (137) 2 hours ago            python_container
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

This output shows two containers:

  • python_container: This container has the ID a3d5e6f4b1a7 based on the Python image. It was created 2 days ago and is currently exited (with exit code 137, indicating an interrupted process).

  • nginx_container: This container has the ID b8e4c7d3e2f9 based on the nginx image. It was created 4 days ago and has been running for 1 hour.

By default, docker container ls only displays running containers. Using the extra flag -a provides the container that can be helpful for troubleshooting and cleanup.

The -f flag or --filter the option allows you to filter the output based on various search criteria. Let’s explore the Docker container ls filter with examples:

1. Filter by Container Name

You can use name a filter to match the Docker container name partially.

docker container ls -f name=<container_name>

Replace the <container_name> with the string name

Example: Filtering the Containers Starting with “nginx”:

This command will list all containers whose names contain the string “nginx.”

docker container ls -f name=nginx

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

Additional Links:

2. Filter by Container ID

You can use id filter to match the Docker container ID.

docker container ls -f id=<container_id>

Replace the <container_id> with the actual ID of the container

Example: Filtering the Containers Starting with “nginx”:

Assuming the container ID b8e4c7d3e2f9 exists, this command will only display that specific container.

docker container ls -f id=b8e4c7d3e2f9

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

3. Filter by Container Status

You can use status filter to filter out the container based on the container status:

docker container ls -f status=<status>

You can replace the status with the desired status, here are possible options

createdLists containers in the created state not yet started.
runningLists only running containers.
pausedLists only paused containers using docker pause
restartingLists restarting containers due to policy.
exitedLists the exited containers using docker stop
removingLists the containers that are in the process of remove with docker rm
deadLists the “defunct” containers that can’t be (re)started, only removed.

Docker Container ls Filter - Status Options

Example: Filtering the Running Containers:

This command will only display containers that are currently running.

docker container ls status=running

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

4. Filter by Container Exited Code

You can use exited a filter to match the Docker container with a specific exited code.

docker container ls -f exited=<exited_code>

Replace the <exited_code> with the actual exited code

Example: Filtering the Containers Exited Code 0:

docker container ls -f exited=0

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
a3d5e6f4b1a7   python     "python app.py"          2 days ago     Exited (0) 2 hours ago            python_container

5. Filter by Container Exited Signal

You can use exited a filter to match the Docker container with a specific exited signal.

docker container ls -f exited=<exited_signal>

Replace the <exited_signal> with the actual exited signal. Let’s filter the exited signal 137 means `SIGKILL(9) signal

Example: Filtering the Containers Exited Signal 137:

docker container ls -f exited=0

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
a3d5e6f4b1a7   python     "python app.py"          2 days ago     Exited (0) 2 hours ago            python_container

Master Docker Security - Without the Guesswork

Get the exact steps top engineers use to secure production containers. 100% free.

Docker-Defence-EBook

Advance: Docker Container ls Filter

1. Filter by Container Label

You can use the label filter to match Docker containers based on arbitrary key-value pairs.

docker container ls -f label=<label_name>=<label_value>

Replace <label_name> with the desired label key and <label_value> with the desired label value.

Example: Filtering Containers by Label “app=web”:

docker container ls -f label=app=web

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

2. Filter by Container Ancestor

You can use the ancestor filter to match Docker containers that share a given image as an ancestor. The ancestor filter matches containers based on its image or a descendant of it.

The filter supports the following image representations:

  • image
  • image:tag
  • image:tag@digest
  • short-id
  • full-id

Example: Filtering Containers by Ancestor “ubuntu:latest”:

docker container ls -f ancestor=ubuntu:latest

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

3. Filter by Container Creation Time

You can use the before and since filters to match containers created before or after a given container ID or name.

beforefilter shows only containers created before the container with a given ID or name.
sincefilter shows only containers created since the container with a given ID or name

Docker Container ls Filter - Creation Time

Example: Filtering Containers Before a Specific Container ID:

docker container ls -f before=<container_id>

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
a3d5e6f4b1a7   python     "python app.py"          2 days ago     Exited (0) 2 hours ago            python_container

4. Filter by Container Volume

You can use the volume filter to match running Docker containers that have mounted a given volume or bind mount.

docker container ls -f volume=<volume_name>

Replace <volume_name> with the name of the volume or bind mount.

Example: Filtering Containers by Mounted Volume “data”:

docker container ls -f volume=data

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
a3d5e6f4b1a7   python     "python app.py"          2 days ago     Exited (0) 2 hours ago            python_container

5. Filter by Container Network

You can use the network filter to match running Docker containers connected to a given network.

docker container ls -f network=<network_name>

Replace <network_name> with the name of the network.

Example: Filtering Containers by Connected Network “my_network”:

docker container ls -f network=my_network

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

Additional Links:

6. Filter by Container Publish or Expose

You can use the publish or expose filter to match containers that publish or expose a given port.  The default protocol is tcp when not specified.

docker container ls -f publish=<port>
docker container ls -f expose=<port>

Replace <port> with the desired port number.

Example: Filtering Containers by Exposed Port 80:

docker container ls -f expose=80

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

Example: Filtering Containers by Exposed Port UDP 80:

docker container ls --filter publish=80/udp

7. Filter by Container Isolation

You can use the isolation filter (Windows daemon only) to match containers based on their isolation mode.

docker container ls -f isolation=<isolation_mode>

Replace <isolation_mode> with one of the following values: default, process, or hyperv.

Example: Filtering Containers by Isolation Mode “hyperv”:

docker container ls -f isolation=hyperv

CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS     NAMES
b8e4c7d3e2f9   nginx      "nginx -g 'daemon of…"   4 days ago     Up 1 hour                 80/tcp    nginx_container

8. Filter by Container Task Status

You can use the is-task filter to match containers that are tasks for a service.

docker container ls -f is-task=<true_or_false>

Replace <true_or_false> with either true or false.

Example: Filtering Containers that are Tasks:

docker container ls -f is-task=true

Conclusion

As the number of Docker containers increases, the docker contaienr ls command can become essential for maintaining the containers.

By using the Docker container ls filter effectively you can maintain the Docker containers at scale for complex troubleshooting and management tasks such as finding a container using a network or finding a container created after a certain time.

For further exploration of Docker commands and advanced filtering options, refer to the official Docker documentation: https://docs.docker.com/

CTA image for Docker-Defence-EBook
Kashyap Merai

Kashyap Merai

Kashyap Merai, a Certified Solution Architect and Public Cloud Specialist with over 8 years in IT. He helped startups in Real Estate, Media Streaming, and On-Demand industries launch successful public cloud projects.

Passionate about Space, Science, and Computers. He also mentors aspiring cloud engineers, shaping the industry's future.

Connect with him on LinkedIn to stay updated on cloud innovations.

Related Posts