Most docker users often need to quickly access information about their containers, including details like the latest containers, disabling truncation, enabling quiet mode, viewing container size, implementing advanced filtering with -f option, and customizing output.
Understanding how to efficiently list docker containers can greatly enhance workflow and improve container management.
In this article, we will examine into the various features and commands that can help you effectively navigate and monitor your docker containers.

List Running containers

To list all running docker containers or containers that are in “Up” state, either of the below commands can be used.

docker ps
docker container ls

Example output of these commands is

  CONTAINER ID        IMAGE               COMMAND       CREATED          STATUS           PORTS          NAMES
  610db616dae5                     ae33e5ecb4d1          “-it /bin/sh”             3 hours ago              Up 5 seconds                                   suspicious_rubin

List all containers
To list all docker containers including stopped, use any of the below commands

docker ps -a
docker ps --all
docker ls -a
docker ls --all

Here, all means the containers which are running as well as those which are stopped or exited.

Sample output of any of these commands is shown below

  CONTAINER ID       IMAGE                 COMMAND       CREATED            STATUS                   PORTS      NAMES
  9b9f84abb05a                    ae33e5ecb4d1              “/bin/sh”                 3 hours ago                Exited (0) 3 hours ago                         goofy_wozniak
  b6bec215c01d                    ae33e5ecb4d1             “-it sh”                       3 hours ago                Created                                                 agitated_jepsen
  6ba38713b56d                    ae33e5ecb4d1             “-it /bin/sh”            3 hours ago                Created                                                 reverent_joliot
  610db616dae5                    ae33e5ecb4d1             “-it /bin/sh”            3 hours ago                Up 5 seconds                                        suspicious_rubin

Display last n containers
To list the last or latest n containers, use any of the below commands

docker ps --last 2
docker ps -n 2

docker container ls - -last 2
docker container ls -n 2

All of these commands will list the last 2 containers irrespective of their status.
Example output is

  CONTAINER ID     IMAGE      COMMAND       CREATED      STATUS                           PORTS      NAMES
  d0af0fddd0fe                  app                 “npm start”               3 hours ago         Exited (0) 36 minutes ago                            quirky_lewin
  a2bfae7d4013                  app                 “npm start”               3 hours ago         Created                                                           nostalgic_stonebraker

Display latest container
To get the most recent or latest container irrespective of status, use any of the below commands

docker ps - -latest
docker ps -l

docker container ls - -latest
docker container ls -l

Output will be the details of the latest single container details

  CONTAINER ID     IMAGE          COMMAND       CREATED       STATUS           PORTS      NAMES
  a07c410f736d                  react-app        “npm start”          4 hours ago     Exited (0) 36 minutes ago              cool_bouman


Display container IDs
If you are concerned only with the ids of the containers and don’t need other details such as name, status etc., then use the quiet option as shown below.

# display ids of last 3 containers
docker ps -n 3 - -quiet
# display ids of all containers
docker ps -a - -quiet
# display id of latest container
docker ps -l -q

# display ids of last 2 containers
docker container ls - -last 2 - -quiet
# display ids of last 2 containers
docker container ls -n 2 -q

Filtering list of docker containers
Both docker container ls and docker ps command have a --filter or -f option to filter out the list of containers on the basis of criteria such as id, name, status, exit status code etc.
Examples are

A. Display running containers

docker ps - -filter "status=running"
docker ps -f "status=running"
docker ps -f status=running

docker container ls - -filter "status=running"
docker container ls -f "status=running"

Possible values for status are created, running, restarting, removing, paused, exited and dead.

So, to list only stopped containers, using -f status=exited.

B. Display containers with specific id

docker ps - -filter "id=d0af0fddd0fe"
docker ps -f "id=d0af0fddd0fe"
docker ps id=d0af0fddd0fe

docker container ls - -filter id=d0af0fddd0fe
docker container ls -f "id=d0af0fddd0fe"

Note that if the container is not in running state, then you need to use -a or --all flag also since by default ps and container ls commands display only running containers.
C. List docker containers with name

docker ps - -filter "name=nice_burnell"
docker ps -f "name=sleepy_bhabha"
docker ps name=silly

docker container ls - -filter name=nice
docker container ls -f "name=sleepy"

Note that you can also use a part of the name.
Use option -a or --all as well, if the container is not in running state.
D. List containers with exit code
Following commands will display containers with a given exit code.

docker ps -a -f exited=0
docker ps -a -f "exited=127"

docker container ls -a -f exited=0
docker container ls -a -f "exited=127"

E. Display container size
Use -s option with docker container ls or docker ps commands to display the amount of disk space occupied by each container.
Example,

# display space used by container with id
docker ps -a -s -f id=d0af0fddd0fe

Output is

  CONTAINER ID        IMAGE          COMMAND     CREATED         STATUS                                         NAMES                    SIZE
  a07c410f736d               react-app      “npm start”        4 hours ago       Exited (127) 36 minutes ago         cool_bouman           8B(vitual 510MB)

Filtering output
Till now, you have seen that all commands display all the details of containers except -q option which list only container ids.
What if you are interested in only some of the fields such as id, name and status of containers.

There is a --format option which can be used with pre-defined placeholders to display the fields and in the required format.
Example,

docker ps -a --format "{{.ID}} - {{.Status}}"

Here, {{.ID}} and {{.Status}} are the placeholders where id and status of the containers will be displayed respectively.

Output of above command will be

d0af0fddd0fe – Exited (127) 4 hours ago
b6bec215c01d – Created
6ba38713b56d – Created
610db616dae5 – Created
a346184b6562 – Exited (0) 29 hours ago
bfa6b2e40f64 – Up 2 hours

Note that there are no headers. To list the details along with header using --format with table option.

Other placeholders supported by --format are below

PlaceholderDescription
{{.Command}}Command executed in container
{{.CreatedAt}}Time of creation of container
{{.ID}}Id of container
{{.Image}}Id of image
{{.Labels}}Labels assigned to container
{{.Mounts}}Mounts in the container
{{.Name}}Name of container
{{.State}}State of container. Example, running, exited etc.
{{.Status}}Container duration and health status
{{.Size}}Disk size used by the container

Summary
1. To list docker containers, there are two commands

docker container ls
docker ps

Both these two commands display the list of running containers only.

2. List all containers with --all or -a option.

3. List last n containers, use --last n or -l n option.

4. List latest container with --latest or -l option.

5. List all stopped containers using –filter or -f option with status filter

docker ps -f status=running
docker container ls -f status=running

6. Use -s option to list space used by containers.

7. To list only specific fields and to customize the layout of container details use --format option with pre-defined placeholders.

To know all the available options with docker ps and docker container ls commands, use --help option after these. Visit this link for ps and this for ls command documentation.

Conclusion

Considering all points discussed, the docker command to show running containers provides imperative information about the containers currently active in the system.
Understanding how to list the latest containers, disable truncation for better visibility, enable quiet mode for concise output, view container sizes, apply advanced filtering with the -f option, and extract customized information ensures efficient container management.
By mastering these features, users can effectively monitor and manage their docker containers with precision and ease.