How to List Containers in Docker?

Docker containers are the runtime environments that provide applications to test, build, and deploy in a virtual interface. The container acts like a virtual machine with limited resources that use the host operating system.

The purpose of this guide is to demonstrate different options for displaying the list of containers in Docker. The content of this article is as follows:

Let’s start with the first method.

How to Display the List of Containers in Docker?

The container list contains the information of “Container ID”, “Image”, “Command”, “Created”, “Status”, “Ports”, and “Name”. The basic syntax to visualize the list of docker containers is given below:

Syntax:

$ docker container ls [options]

In the above syntax, the “ls” command displays the list of existing docker containers in the system.

To explore different options of docker, users can utilize the “help” utility as below:

$ sudo docker container ls --help

Display the List of All Containers

To display the list of all containers in docker, the “a” option is used that provides the container information by executing the below script:

$ sudo docker container ls -a

The output of the executed command is explained in the below list:

  • Container ID:  A unique identification of each container consisting of alphanumeric strings.
  • Image: It specifies the Docker image utilized for creating the container.
  • Command: It represents the command which is executed in the container.
  • Created: It shows the time when the container is created.
  • Status: It represents the container status.
  • Ports: These refer to the container’s published ports.
  • Name: It specifies the container name.

Latest Created Container in Docker

The “latest” utility is used to display the container that was recently created in docker. To do so, follow the below script:

$ sudo docker container ls --latest

The output returns the recent container created “21 minutes” ago.

Specific Created Containers

To list the specifically created containers, the “n” utility is used by specifying the number of containers. For this, we are listing the latest two containers by executing the below script:

$ sudo docker container ls -n 2

The output returns the latest two created containers.

List Docker Containers by Size

To display the docker containers sorted by size, the “s” option provides the container list based on sizes:

$ sudo docker container ls --latest -s

The output returns the latest created container having a maximum size “77.8MB”.

Sizes of Running and Stopped Containers

Additionally, the user can visualize the sizes of running and stopped containers using the “as” utility. To do so, execute the below script:

$ docker container ls -as

List Containers Having Created Status

To display the container list having “status=created”, the “f” option is used that fetch data based on the query:

$ sudo docker container ls -f "status=created"

Display a List of Containers in Truncated Manner

To display the complete id of the docker container, the “no-trunc” utility removes the limit of exceeding the column’s value:

$ docker container ls --latest --no-trunc

The output shows that the complete “Container ID”, which has a long length.

Alternative Command to List Containers in Docker

The “ps” command is utilized as an alternative to listing the containers in Docker. For this, execute the below script:

$ sudo docker ps -a

The output shows the information of “Container ID”, “Image”, “Command”, “Created”, “Status”, “Ports”, and “Name”.

These are all the possible ways to list containers in docker.

Conclusion

To display the list of containers in docker, execute the “<sudo docker container ls -a>” command. It displays the list of “Container ID”, “Image”, “Command”, “Created”, “Status”, “Ports”, and “Name” that are currently running in the system. This article has explained various options to visualize the containers list in the operating system.