How to List Containers in Docker?

How to List Containers in Docker?
Techiio-author
Written by Nilima PaulFebruary 15, 2022
12 min read
Docker
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will discuss How to List Containers in Docker.

Introduction to Docker List Containers

To list docker compartments, we use 'docker holder ls' or 'docker ps' order. The two orders have similar banners as the two orders work on the same thing for example compartment. It has various banners to get the result according to our necessity as it just shows showing compartments to default. The 'docker ps' order is faster and simpler to type.

Syntax:

$docker container ls --help

$docker ps –help

How to List Containers in Docker?

  • We need to utilize the Docker CLI apparatus to execute the order to list the holders.
  • We have two Docker orders that rundown the compartments.
  • The first is 'docker compartment ls' and the subsequent one is 'docker ps'.
  • Assuming we run any of this order we just get the running holders on that host, nonetheless, the order has various choices that we can indicate to get the rundown of compartments that we want.

Examples of Docker List Containers

Given below are the examples mentioned:

Example #1

We have numerous holders in various states like a few compartments are running and some are in left status or some other state other than running. We need to list every one of the holders whether or not it is running.

a. How about we make a couple of snow capped compartments as beneath:

Code:

$docker run -d alpine sleep 3600
$ docker run -d alpine
$ docker run -d alpine

b. Now, we can run either ‘docker container ls’ or ‘docker ps’ command to get the list of the containers as below:

Code:

$docker container ls
$docker ps

Explanation

  • We have created 3 containers but both commands show only 1 container as by default it shows only running containers.

c. We have to use the ‘–all’ or ‘-a’ flag to list all the containers as below:

Code:

$docker container ls --all
$ docker container ls –a
$docker ps --all
$docker ps -a

Example #2

Let’s assume we have hundreds of containers running on a host and we want to see specific containers, for example, all containers that are exited, have a specific name, id, label, volume, network, etc.

a. Create few containers with labels and names as below:

Code:

$ docker run -d ubuntu sleep 3600
$ docker run -d alpine
$ docker run --label type=db -d redis
$ docker run -d --name test-con1 alpine

b. Let’s start filtering these containers with id first as below:

Code:

$ docker container ls -a -f "id= ebe9557e"
$ docker container ls -a -f "id=eb"

c. We can also filter on the basis of the name of the container as below:

Code:

$docker container ls -a -f "name=test-con1"

d. We can filter the container based on its labels as below:

Code:

$ docker container ls -f "label=type=db"
$ docker container ls -f "label=type"

Explanation

In the above snapshot, the first command shows the containers which have label ‘type=db” and the second command shows the containers which have label “type” as key regardless of the value.

e. We can filter the containers based on its image as below:

Code:

$ docker container ls -a -f ancestor=alpine

Code:

$ docker container ls -a -f ancestor=3fd9065eaf02

f. We want to filter containers running before and after a specific container, we can use before and since filter as below:

Code:

$ docker container ls -a -f before=laughing_carson
$ docker container ls -a -f since=laughing_carson

There are other filters available as well.

Example #3

Get familiar with the utilization of the '- design' choice. To show a particular field of the holder or arrangement the result according to our need. We can design in light of compartment ID, picture ID, order, ports, and so on

a. To show just holder's ID, then, at that point, use - design choice as underneath:

Code:

$docker container ls -a --format "{{.ID}}"

b. If we want to output name with the id then we have to add that field as below:

Code:

$docker container ls -a --format "{{.ID}} {{.Names}}"

c. We can use the ‘\t’ to make more room between the fields as below:

Code:

$ docker container ls -a --format "{{.ID}} \t {{.Names}}"

d. We can use multiple fields at the same time and any symbol if you want as shown below:

Code:

$docker container ls -a --format "{{.ID}} -> \t {{.Names}} -> \t {{.Status}}"

e. We can check labels or any other hidden fields that is not shown by default when we list the containers.

Code:

$ docker container ls -a --format "{{.ID}} \t {{.Names}} \t {{.Labels}} \t {{.Size}}"

Explanation

In the above snapshot, only one container has a label i.e. type=db.

f. We can see that the output does not have column headers like ID, Name, Label, etc. We have to use the ‘table’ directives as below:

Code:

$docker container ls -a --format "table {{.ID}} \t {{.Names}} \t {{.Labels}} \t {{.Size}}"

It gives us a cleaner output as well.

Example #4

We want to list last n created containers. We can do it by using the ‘–last’ or ‘-n’ option as shown below:

Code:

$ docker container ls -n 2

The above example shows the last 2 created containers and also includes all containers.

Example #5

We can use the ‘-l’ flag to list the latest created container. It shows all the containers regardless of their state without specifying the ‘-a’ flag.

Code:

$docker container ls -l

Example #6

Sometimes we have issues with the output due to screen size or more columns so by default Docker daemon truncated the output of some columns as below:

Code:

$docker container ls

Explanation

In the above snapshot, we see that the ‘COMMAND’ column has been truncated.

We can use the ‘–no-trunc’ option to display full value of that column as below:

Code:

$docker container ls --no-trunc

Example #7

If we want to list container ID only then we can use the option ‘–quiet’ or ‘-q’. It is similar to using the ‘–format’ option with ID only. We can pipe it to the ‘wc’ command to count the containers as shown below:

Code:

$docker container ls --quiet
$ docker container ls  -a –quiet
$ docker container ls -a –quiet | wc

Example #8

If we want to display the size of the containers, we have to use the ‘–size’ or ‘-s’ option as shown below, however, we can output the same thing using the ‘format’ option as well but this command it is quicker.

Code:

$ docker container ls -a  --size

Conclusion

We can indicate various choices to get the rundown of holders anyway most helpful choices are the 'channel' and the 'design' choice. More often than not we utilize the 'docker ps' order as we need to type less.

Docker
DockerListContainers
Docker secrets
DevOps
Docker file
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance