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

Written by

Reviewed by

Last updated: June 7, 2024

Expert verified

SVG Image

TL;DR

To list docker containers in Linux, follow these steps:

  1. Open your terminal.
  2. Use sudo su to gain necessary permissions.
  3. Run docker ps -a to list all containers in all states.

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

Managing Docker containers can sometimes feel overwhelming, especially when your system hosts multiple applications. Fortunately, there’s a solution to keep things under control. This post will show you how to list Docker containers in Linux, making it easier to monitor, manage, and troubleshoot your containerized applications. You’ll learn various methods for listing Docker containers, filtering and formatting outputs, and using advanced techniques to get the most out of your Docker setup.

Why List Docker Containers?

Listing Docker containers helps you manage and monitor your containerized applications effectively. Here are some key reasons why you should regularly list your Docker containers:

  • Monitor Active Containers: By listing running containers, you can see which applications are currently active and check their status, resource usage, and performance.
  • Identify Stopped Containers: Listing stopped containers helps you find containers that are no longer running. This is useful for cleaning up unused containers that take up space and resources.
  • Troubleshoot Issues: When you encounter issues with your applications, listing containers provides detailed information about each container. This information can help you diagnose and fix problems quickly.
  • Manage Multiple Containers: In environments where you run many containers, listing them helps you keep track of all your applications. You can easily see which containers are running, stopped, or need attention.
  • Optimize Resource Usage: By regularly listing and reviewing your containers, you can identify and remove unnecessary containers, freeing up system resources and improving performance.

How to List Containers in Docker

To list containers in Docker, open your terminal and ensure you have the necessary permissions by using sudo su. Then, run the command docker ps to list all running containers. If you want to see all containers, including those that are stopped or exited, use the command docker ps -a. This will display comprehensive details about each container’s status and state.

Prerequisites

Before you start using Docker, you need to ensure your system meets the necessary requirements. Here’s what you need to know about the system requirements for Docker.

  • Debian: Versions 9 (Stretch) and 10 (Buster)
  • CPU: A modern, multi-core processor
  • RAM: At least 4GB of RAM; 8GB or more is recommended for better performance
  • Storage: A minimum of 10GB free disk space; SSD is preferred for faster performance

1. List All Containers (Running and Stopped)

The docker ps -a command is a powerful tool that provides a comprehensive list of all containers, offering valuable insights for troubleshooting, auditing, or cleaning up unused containers.

  1. Start by opening your terminal. Ensure you have sufficient permissions to interact with Docker. Running Docker commands typically requires root or sudo privileges.
sudo su
logging as sudo user
  1. Execute the docker ps -a command to list all containers.
docker ps -a
  • docker ps: Lists all running containers.
  • -a (or –all): Modifies the command to include all containers, not just those currently running. This flag ensures you see containers in all states: running, stopped, exited, etc.
listing all docker containers

2. 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:
docker ps
  1. This command will display essential information about each running container, including its Container ID, Names, Status, and exposed Ports.
listing docker containers

3. Listing Stopped Containers

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:
docker ps -a --filter status=exited
  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

Filtering and Formatting Container Listings

When managing Docker containers, it’s helpful to filter and format the list of containers to see only the information you need. Docker provides powerful options to do this: --filter and --format.

1. Filtering Containers

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:
docker ps --filter status=running
  1. This will display a list of containers that are currently running.
filtering containers to list only running ones

2. 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:
docker ps --format "ID: {{.ID}}\tName: {{.Names}}\tStatus: {{.Status}}"
  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

Advanced Techniques

1. Inspecting Docker Containers

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:
docker inspect container_name_or_id
  1. The output will provide a JSON representation of the container’s configuration, network settings, and more.
getting detailed info about specific container 1

2. Viewing Logs of a Docker Container

Viewing the logs of a Docker container helps you understand what’s happening inside the container, debug issues, and monitor its activity.

  1. Access your command window. To view the logs of a Docker container, use the docker logs command followed by the container ID or name.
docker logs <container_id_or_name>
  • docker logs: The command to retrieve logs from a Docker container.
  • <container_id_or_name>: Replace this with the actual container ID or name whose logs you want to view.
viewing logs of a docker container
  1. To view real-time logs (live updates), use the -f (follow) option:
docker logs -f <container_id_or_name>
viewing realtime logs of a docker container

Best Practices for Managing Docker Containers

Managing Docker containers efficiently ensures smooth operation, security, and scalability. Implementing best practices can help avoid common pitfalls and maintain a healthy container ecosystem. Here are five essential practices to follow for effective Docker container management.

  • 📦 Use Lightweight Base Images: Start with minimal base images to reduce container size, improving performance and security. Smaller images have fewer vulnerabilities and are easier to update and maintain.
  • 🔄 Regularly Update Containers: Keep containers and their dependencies updated to ensure they have the latest security patches and performance improvements. Regular updates prevent exploitation of known vulnerabilities.
  • 🔍 Monitor Container Health: Continuously monitor container health using tools like Prometheus and Grafana. Monitoring helps detect issues early, ensuring timely intervention and maintaining system stability.
  • 🚀 Automate Deployment and Management: Use tools like Docker Compose and Kubernetes to automate container deployment and management. Automation simplifies scaling, updating, and maintaining containers, reducing human error and improving efficiency.
  • 🔒 Implement Security Best Practices: Run containers with the least privileges, use secure configurations, and regularly scan for vulnerabilities. Implementing security best practices protects against attacks and ensures a robust container environment.

List Docker Container: Wrapping Up

In this article, I covered essential methods for listing Docker containers in Linux, including how to list running and stopped containers, filter and format container listings, and use advanced techniques like inspecting containers and viewing logs.

For further learning, I recommend exploring these topics:

  • Learn how to fix the “Command Not Found: Docker Compose” error, which helps you resolve issues that can prevent you from managing multi-container applications effectively.
  • Understand methods to find Docker container IP addresses, crucial for configuring networks and ensuring proper communication between your containers.
  • Explore how to implement Docker restart policies to ensure your containers automatically restart after failures, enhancing the reliability and resilience of your Docker environment.

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

How to Apt Remove Package Using Apt Package Manager[2 Proven Methods]

Leave a Reply

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

Read next