Listing Docker containers

Introduction

Docker is an open-source platform that simplifies the process of developing, shipping, and running applications by using containers. Containers allow developers to package applications and their dependencies together, ensuring a consistent environment across development, testing, and production stages. One of the most common tasks when working with Docker is managing and listing containers. This article will provide a comprehensive guide to listing Docker containers, including various options and filters to customize the output.

The ‘docker ps’ Command

The primary command for listing Docker containers is ‘docker ps’. By default, this command shows all running containers. To list containers, simply open a terminal and type:

docker ps

The output displays essential information about the containers, including their container ID, image, command, created time, status, ports, and names.

Listing All Containers

To list all containers, including those that are stopped or exited, use the ‘-a’ or ‘–all’ option:

docker ps -a

Displaying Containers with Specific Status

You can filter the container list by their status using the ‘–filter’ option. For example, to display only the exited containers, use:

docker ps --filter "status=exited"

Other status options include ‘created’, ‘restarting’, ‘running’, and ‘paused’.

Listing Containers by Name or ID

To list containers by their name or ID, use the ‘–filter’ option with the ‘name’ or ‘id’ keyword. For example:

docker ps --filter "name=my_container_name"

Or:

docker ps --filter "id=my_container_id"

Formatting the Output

The ‘docker ps’ command allows you to customize the output by using the ‘–format’ option. This feature helps you display only the necessary information, making the output more readable. For example, to display only the container ID and names, use:

docker ps --format "{{.ID}}: {{.Names}}"

You can also format the output as a table by using the ‘table’ keyword:

docker ps --format "table {{.ID}}\t{{.Names}}"

Limiting the Number of Containers Displayed

If you have many containers and want to display only a specific number of them, use the ‘–last’ or ‘-n’ option. For example, to display the last 5 containers:

docker ps --last 5

Sorting Containers by Columns

To sort the output by specific columns, use the ‘–sort’ option. For example, to sort containers by their creation time in ascending order:

docker ps --sort "created"

To sort the containers in descending order, add a ‘-‘ before the column name:

docker ps --sort "-created"

Conclusion

In this article, we explored various ways to list Docker containers using the ‘docker ps’ command. We covered options for listing all containers, filtering containers by status, name, or ID, formatting the output, limiting the number of containers displayed, and sorting containers by specific columns. Understanding these options will help you manage your Docker containers more effectively and efficiently.

Related Articles