Docker command: ps

Learn how to effectively use the `docker ps` command to list and filter Docker containers. Explore practical examples and customize container information.

Docker command: ps
Docker command: ps

Introduction

As a beginner in Docker, it's important to familiarize yourself with essential Docker commands. One such command is docker ps, which allows you to list the containers running on your system. In this article, we'll explore the docker ps command in-depth, covering its usage, options, and practical examples. Let's dive in!

Understanding Docker PS

The docker ps command is used to list the containers currently running on your Docker host. It provides crucial information about each container, such as the container ID, image used, command executed, and status.

Listing All Containers

To list all the containers running on your Docker host, simply run the docker ps command without any options:

docker ps

This command will display a table with detailed information about each container, including its container ID, image, command, and status.

Filtering Containers

Filtering the output of docker ps allows you to narrow down the results and fetch specific containers based on various criteria. Docker provides multiple filtering options to help you achieve this.

By Container Name

If you know the name of the container you want to list, you can use the --filter flag with the name filter to fetch containers matching that name:

docker ps --filter name=<container_name>

Replace <container_name> with the desired name of the container.

By Container Status

You can also filter containers based on their status. For example, if you want to list all the running containers, you can use the --filter flag with the status filter:

docker ps --filter status=running

Similarly, you can replace running with other status values like exited, created, or restarting to fetch containers with specific statuses.

Combining Filters

Docker allows you to combine multiple filters, making your query more specific. Use the --filter flag multiple times to add additional filters:

docker ps --filter name=<container_name> --filter status=<container_status>

Replace <container_name> with the desired container name and <container_status> with the desired container status.

Displaying Container Information

docker ps provides basic information about running containers, but sometimes you may need more detailed output for troubleshooting or debugging purposes. Docker provides additional flags to customize the information displayed.

Display All Container Information

By default, docker ps only displays the most relevant information about containers. If you want to view all available fields, including the container environment variables, use the --no-trunc flag:

docker ps --no-trunc

This command will display the complete information for each container, allowing you to see all the available fields.

Display Container IDs Only

If you're only interested in the container IDs, you can use the --quiet flag to print only the container IDs:

docker ps --quiet

The command will display the container IDs, one per line, without any additional information.

Practical Examples

Let's explore some practical examples of using docker ps to get a better understanding of its capabilities.

Example 1: List All Containers

To list all the containers on your Docker host, simply execute the following command:

docker ps

The command will display a table with detailed information about each container, including its container ID, image, command, and status.

Example 2: List Container by Name

If you know the name of a specific container and want to fetch its details, run the following command:

docker ps --filter name=<container_name>

Make sure to replace <container_name> with the actual name of the container.

Example 3: List Running Containers

If you're only interested in the running containers, execute the following command:

docker ps --filter status=running

This command will fetch all the containers that are currently running on your Docker host.

Conclusion

The docker ps command is an essential tool for managing and monitoring Docker containers. By mastering its usage and understanding the available options, you'll have more control over your Docker environment, enabling you to efficiently list and filter containers based on your needs.

Now that you have a good grasp of the docker ps command, it's time to explore other Docker commands and dive deeper into the world of containerization. Happy containerizing!