JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Listing containers in Docker

Author:JIYIK Last Updated:2025/03/25 Views:

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values.

List all running containers in Docker

We will start by listing the running containers. To do this, we can use docker psthe command.

$ docker ps

This command lists all of the currently running containers, which can be seen under the STATUS column in the example output below. As shown here, we can also determine the approximate amount of time a container has been up and running.

CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
1c955bac1a84   ubuntu    "bash"                   About a minute ago   Up About a minute             musing_morse
1296f9b9d330   nginx     "/docker-entrypoint.…"   2 minutes ago        Up 2 minutes        80/tcp    distracted_napier

We can also use docker container lsthe command which returns the same output.

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
1c955bac1a84   ubuntu    "bash"                   14 minutes ago   Up 14 minutes             musing_morse
1296f9b9d330   nginx     "/docker-entrypoint.…"   15 minutes ago   Up 14 minutes   80/tcp    distracted_napier

In addition to STATUS, the following details are returned.

  • CONTAINER ID, which is a unique identifier for the container;
  • CONTAINER IMAGE, which is the image we use to build the container;
  • COMMAND responsible for running the container;
  • Port mapping between host and container.

List running and exited containers in Docker

In addition to listing running containers, we can also list running and exited containers. We need to append -athe tag to the command we used earlier.

By default, these commands only show running containers; however, using this tag you get commands to list exited containers as well.

Order:

$ docker ps -a

Output:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                          PORTS     NAMES
38086474cb6c   debian    "bash"                   4 minutes ago    Exited (0) 2 minutes ago                  epic_jackson
1c955bac1a84   ubuntu    "bash"                   52 minutes ago   Exited (0) About a minute ago             musing_morse
1296f9b9d330   nginx     "/docker-entrypoint.…"   53 minutes ago   Up 58 seconds                   80/tcp    distracted_napier

In the output under the Status column above, you can notice that the first two containers are not running, while the last container is running.

List a specific number of containers in Docker

We can also display a specific number of containers in our system. Let’s say we want to display only the first two containers. We can -ndo this by using the -n tag alongside the command we used above.

This will display both running and exited but not more than the specified number.

Order:

$ docker container ls -n 2

Output:

CONTAINER ID   IMAGE     COMMAND   CREATED             STATUS                      PORTS     NAMES
38086474cb6c   debian    "bash"    31 minutes ago      Exited (0) 29 minutes ago             epic_jackson
1c955bac1a84   ubuntu    "bash"    About an hour ago   Exited (0) 27 minutes ago             musing_morse

Likewise, using docker psthe command will also list the exact number of containers we specify below. Here is how we can achieve it.

Order:

$ docker ps -a -n 2

Output:

CONTAINER ID   IMAGE     COMMAND   CREATED             STATUS                      PORTS     NAMES
38086474cb6c   debian    "bash"    33 minutes ago      Exited (0) 31 minutes ago             epic_jackson
1c955bac1a84   ubuntu    "bash"    About an hour ago   Exited (0) 29 minutes ago             musing_morse

List only exited containers in Docker

Using filters, we can also list only containers that are not currently running. Use -fthe <container-name> tag to specify the condition we want to meet, in this case, we only want containers with an exit status returned.

We can also add multiple filters if we want containers returned based on other things. This is how we can list containers with exit status.

Order:

$ docker container ls --filter "status=exited"

Output:

CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
38086474cb6c   debian    "bash"    51 minutes ago   Exited (0) 49 minutes ago             epic_jackson
1c955bac1a84   ubuntu    "bash"    2 hours ago      Exited (0) 48 minutes ago             musing_morse

List recently created containers in Docker

It is also possible to return the latest container using -latestthe tag. This will return the current container that we most recently created.

Order:

$ docker container ls --latest

Output:

CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
38086474cb6c   debian    "bash"    55 minutes ago   Exited (0) 53 minutes ago             epic_jackson

List containers by ID in Docker

We can also list containers based on their IDs, and we can -qdo this using , also known as the quiet option, as shown below.

Order:

$ docker container ls -q

Output:

 d780996c499a
f7509cd49142
72c8debe5efa

List containers and their sizes in Docker

We can also list the containers next to their size. We can identify those that are taking up the maximum memory size, among other details.

We can -sdo this by using the tag, also known as the size tag.

Order:

$ docker container ls -s

Output:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                                 NAMES               SIZE
d780996c499a   4ac4842c584e   "/opt/sonarqube/bin/…"   3 minutes ago   Up 3 minutes   9000/tcp                                              amazing_benz        73.2kB (virtual 520MB)
f7509cd49142   5285cb69ea55   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   27017/tcp                                             boring_wilson       0B (virtual 698MB)
72c8debe5efa   rabbitmq       "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp   determined_curran   0B (virtual 221MB)

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Tagging images with Docker and Docker Compose

Publish Date:2025/03/25 Views:70 Category:Docker

When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima

Creating a database user with Docker Postgres

Publish Date:2025/03/24 Views:166 Category:Docker

When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save

Adding unsafe registry keys in Docker

Publish Date:2025/03/24 Views:130 Category:Docker

While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer

List all images in Docker Registry V2

Publish Date:2025/03/24 Views:131 Category:Docker

After several iterations, Docker Registry was upgraded from version 1 to version 2. Especially being new, some commands needed to be included or adequately documented on its official documentation website. An example is getting a list of im

Executing multiple commands in Docker-Compose

Publish Date:2025/03/24 Views:152 Category:Docker

Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Docker-compose is an advanced, must-have tool for managing multi-co

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial