How to List Docker Containers in Linux [5 Effective Methods]

Written by

Reviewed by

Last updated: July 26, 2023

Expert verified

SVG Image

TL;DR

To list docker containers in Linux, you can try these methods:

  1. Listing Running Containers: Get a quick overview of running containers with essential information like Container ID, Names, Status, and Ports using docker ps command.
  2. Displaying Container Details: Inspecting Containers: Access detailed container information in JSON format by using docker inspect <container_name_or_id>.
  3. Filtering Containers by Criteria: Focus on specific containers based on criteria like status=running with docker ps –filter status=running.
  4. Sorting and Formatting Output: Customize listing output with specific details using docker ps --format "ID: {{.ID}}\tName: {{.Names}}\tStatus: {{.Status}}".
  5. Listing Stopped Containers: Cleanup and Maintenance: Identify and remove stopped containers to free up resources with docker ps -a --filter status=exited.

Listing Docker containers may seem straightforward, but it comes with challenges. Be mindful of common errors like “No Containers Listed,” “Access Denied,” “Unable to Connect to Docker Daemon,” “Container Names vs. Container IDs,” and “Incorrect Filter Syntax.” By familiarizing yourself with these issues, you can navigate Docker more effectively and enhance your container management skills.

Read the guide below to learn different methods to list docker containers in Linux and common errors that can occur during the process.

Docker containers have revolutionized the world of software development, enabling developers to package applications with all their dependencies and run them in isolated environments. Managing these containers efficiently is crucial for ensuring smooth operations and optimal resource utilization. In this comprehensive guide, I will explore different methods to list docker containers in Linux and common errors that can occur while listing containers.

How to List Docker Containers in Linux

To list Docker containers in Linux effectively, use methods like Listing Running Containers for a quick overview, Displaying Container Details for in-depth insights, Filtering Containers by Criteria for focused results, Sorting and Formatting Output for customization, and Listing Stopped Containers for cleanup. 

1. Listing Running Containers

When you need a quick overview of all the running containers on your system, listing running containers is the way to go. It helps you monitor active containers and provides essential information like Container ID, Names, Status, and exposed Ports, ensuring efficient management of your containerized applications. Follow these steps:

  1. Open your Terminal window.
opening terminal 38
  1. Enter the following command:
<strong>docker ps</strong>
  1. This command will display essential information about each running container, including its Container ID, Names, Status, and exposed Ports.
listing docker containers

2. Displaying Container Details

Sometimes, you may need more detailed information about a specific container. By using the docker inspect command with the Container ID or Name, you can access a JSON representation of the container’s configuration and network settings, aiding in troubleshooting and debugging tasks. To inspect a container, do the following:

  1. In your command prompt, use the docker inspect command, followed by the Container ID or Name:
<strong>docker inspect container_name_or_id</strong>
  1. The output will provide a JSON representation of the container’s configuration, network settings, and more.
getting detailed info about specific container 1

3. Filtering Containers by Criteria

Filtering containers can be incredibly useful when you want to focus on specific containers based on certain criteria. It allows you to focus on relevant containers, such as running or stopped ones, and filter by names or other properties, facilitating effective monitoring and management. Here’s how you can do it:

  1. Launch your Terminal, use the docker ps command with the --filter flag and the desired filter, such as status=running:
<strong>docker ps --filter status=running</strong>
  1. This will display a list of containers that are currently running.
filtering containers to list only running ones

4. Sorting and Formatting Output

Customizing the output of your container listing can make it easier to read and focus on specific details. By using the –format flag with a custom Go template, you can display specific container details like Container ID, Name, and Status, making the output more concise and tailored to your needs. Here’s how you can do it:

  1. Enter your command window and type the following command:
<strong>docker ps --format "ID: {{.ID}}\tName: {{.Names}}\tStatus: {{.Status}}"</strong>
  1. This will format the output to display only the specified information for each container, making it more readable and concise.
listing containers info in customized format

5. Listing Stopped Containers: Cleanup and Maintenance

Over time, you may accumulate stopped containers that are no longer needed, consuming valuable resources. Listing stopped containers can help you identify and clean them up efficiently. Follow these steps:

  1. Open your Terminal and enter the following command:
<strong>docker ps -a --filter status=exited</strong>
  1. This command will list all containers, including the ones that have exited or stopped. You can now review this list and decide which containers to remove to free up resources.
listing all exited or stopped containers

5 Common Errors When Listing Docker Containers

While listing Docker containers is a routine task for developers, it’s not without its challenges. By being aware of these common errors and their resolutions, you can navigate the challenges of listing Docker containers more effectively and maximize the benefits of containerization in your development workflow. Follow these steps:

  • 🚫 No Containers Listed: When you run the docker ps command, you might encounter an empty list with no containers listed. This error usually occurs when no containers are currently running on your system. It’s essential to verify that you have active containers before attempting to list them. Start a container with docker run or check if a running container has exited. Once you have running containers, the docker ps command will display the relevant information.
  • 🔐 Access Denied: Receiving an “Access Denied” message when listing containers can be frustrating. This error typically arises due to insufficient permissions to execute Docker commands. To resolve this, ensure that you are part of the Docker user group or have the necessary privileges to interact with the Docker daemon. On Linux, consider adding your user to the docker group using sudo usermod -aG docker $USER. For Windows users, ensure you are using an elevated command prompt or have administrative access.
  • 🔌 Unable to Connect to Docker Daemon: If you encounter an error stating “Cannot connect to the Docker daemon,” it indicates a connection problem between the Docker client and the Docker daemon. First, check if the Docker daemon is running using docker info or docker version. If the daemon is not running, start it using sudo systemctl start docker (on Linux) or via the Docker Desktop application (on Windows/Mac). Additionally, verify that you have the necessary network access to communicate with the Docker daemon.
  • 🔄 Container Names vs. Container IDs: It’s easy to confuse container names with their unique container IDs when listing containers. Mistaking one for the other can lead to errors or unexpected results. Remember that container names are human-readable, while container IDs are long hexadecimal strings. To list containers using their names, use docker ps -a --filter "name=<container_name>". For listing by container IDs, use docker ps -a --filter "id=<container_id>". Cross-reference the container names and IDs using docker ps -a to avoid confusion.
  • Incorrect Filter Syntax: When using the --filter flag to list containers based on specific criteria, improper syntax can result in errors. Ensure that you use the correct key-value pair syntax for filtering. For instance, to list only running containers, use docker ps --filter "status=running". To filter by a specific label, use docker ps --filter "label=my_label=desired_value". Double-check the filter criteria against the available options to avoid syntax-related errors.

To Sum Up

In this guide, I have provided a comprehensive overview of various methods to list Docker containers in Linux effectively. However, be mindful of common errors such as encountering an empty list, access denied issues, connection problems with the Docker daemon, confusion between container names and IDs, and incorrect filter syntax.

As you continue your Docker journey, consider exploring advanced topics such as Docker networking, Docker volumes to manage data persistence, and Docker Compose to orchestrate multi-container applications. Remember, the world of Docker is vast and ever-evolving, so stay curious, keep learning, and explore the endless possibilities of containerization.

Frequently Asked Questions

Can I list containers that were recently stopped but are no longer available in docker ps output?

Yes, you can list containers that were recently stopped but are no longer visible in the docker ps output. By using the docker ps -a --filter status=exited command, you can view a comprehensive list of all containers, including those that have exited or stopped. The --filter status=exited option filters the containers based on their exit status, providing you with a complete history of recently stopped containers. This is especially useful for monitoring and maintaining container lifecycles even after they have completed their tasks or been manually stopped.

How can I list containers without displaying their header information?

To list containers without the header information, utilize the --no-trunc flag with the docker ps command. This flag ensures that the output does not truncate any data, making it easier to read and process the container information. With the header removed, the listing will only contain rows of container details, providing a cleaner and more concise display. This is particularly handy when you need to parse or process the listing programmatically or for a clearer view of container metadata without any additional clutter.

What is the impact of listing containers on overall system performance?

Listing containers has a minimal impact on overall system performance. The docker ps command primarily involves querying metadata about the containers, which is a lightweight and quick process. The command accesses the Docker daemon’s internal database to fetch container information, resulting in negligible resource consumption. It doesn’t cause any significant computational load or affect the running containers’ performance. As a result, listing containers is considered a low-resource operation, making it safe to use even in production environments with multiple containers.

How can I list only the most recently created containers?

To list only the most recently created containers first, use the docker ps command with the --latest flag. This flag sorts the container listing based on their creation time, placing the most recently created containers at the top of the list. By doing so, you can quickly identify and focus on the latest additions to your container ecosystem. The --latest option is particularly beneficial when you need to keep track of recently deployed or started containers, enabling you to monitor the latest activities within your Docker environment more efficiently.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Update Debian Linux [4 Effective Methods]

Next Post

Uninstall Packages With Apt Package Manager[2 Proven Methods]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next