EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Docker List Containers

Home » Software Development » Software Development Tutorials » Docker Tutorial » Docker List Containers

Docker List Containers

Introduction to Docker List Containers

In order to list docker containers, we use ‘docker container ls’ or ‘docker ps’ command. Both commands have the same flags as both commands operate on the same thing i.e. container. It has different flags to get the output as per our requirement as it only shows running containers by default. The ‘docker ps’ command is quicker and easier to type.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

$docker container ls --help

Docker List Containers 1

$docker ps –help

Docker List Containers 2

How to List Containers in Docker?

  • We have to use the Docker CLI tool to execute the command to list the containers.
  • We have two Docker commands that list the containers.
  • The first one is ‘docker container ls’ and the second one is ‘docker ps’.
  • If we run any of this command we only get the running containers on that host, however, the command has different options that we can specify to get the list of containers that we need.

Examples of Docker List Containers

Given below are the examples mentioned:

Example #1

We have multiple containers in different states like some containers are running and some are in exited status or any other state other than running. We want to list all the containers whether it is running or not.

a. Let’s create a few alpine containers as below:

Code:

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

Output:

Docker List Containers 3

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

Output:

Docker List Containers 4

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

Popular Course in this category
Docker Training (4 Courses, 3 Projects)4 Online Courses | 3 Hands-on Projects | 11+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (4,461 ratings)
Course Price

View Course

Related Courses
Python Training Program (36 Courses, 13+ Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Output:

have to use the ‘--all’ or ‘-a’ flag

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

Output:

Docker List Containers 6

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"

Output:

Let’s start filtering

Docker List Containers 8

Note: We have to use the ‘-a’ flag if the container is not running otherwise it will output nothing. It is recommended to use the ‘-a’ flag while filtering. Also, we can use the short id as well.

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"

Output:

filter on the basis of the name

Note: We can use the short name of the container as well, it will output all containers that match the name as shown below.

Docker List Containers 10

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"

Output:

Docker List Containers 11

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

Output:

on its image

Note: We can use tag, image id, and layer as well.

Code:

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

Output:

Docker List Containers 13

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

Output:

running before and after a specific

There are other filters available as well.

Example #3

Learn the use of the ‘–format’ option. If we want to display a specific field of the container or format the output as per our need. We can format based on container ID, image ID, command, ports, etc.

a. If we want to display only container’s ID, then use –format option as below:

Code:

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

Output:

Docker List Containers 15

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}}"

Output:

Docker List Containers 16

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

Code:

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

Output:

Docker List Containers 17

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}}"

Output:

use multiple fields at the same time and any symbol

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}}"

Output:

check labels or any other hidden fields that is not shown by default

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}}"

Output:

Docker List Containers 20

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

Output:

Docker List Containers 21

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

Output:

Docker List Containers 22

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

Output:

Docker List Containers 23

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

Output:

‘--no-trunc’ option

We can now see the full container ID and command of the container. We can use this option with the other options as well like format:

ID and command

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

Output:

Docker List Containers 26

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

Output:

Docker List Containers 27

Conclusion

We can specify multiple options to get the list of containers however most useful options are the ‘filter’ and the ‘format’ option. Most of the time we use the ‘docker ps’ command as we have to type less.

Recommended Articles

This is a guide to Docker List Containers. Here we discuss how to list containers in docker with examples respectively. You may also have a look at the following articles to learn more –

  1. Dockerfile
  2. Docker Push
  3. Advantages of Docker
  4. What is Docker Swarm?

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Docker Tutorial
  • Docker
    • What is Docker in Linux
    • Docker Architecture
    • Install Docker
    • Advantages of Docker
    • Docker Commands
    • Docker Toolbox
    • Docker Storage Drivers
    • Docker Push
    • Docker run Command
    • Dockerfile
    • Docker Container Linking
    • Docker Stop Container
    • Docker List Containers
    • Docker Registry
    • Docker Volume
    • Docker Export
    • Docker Import
    • Docker Systemd
    • Docker Stack
    • Docker Privileged
    • Docker Pull
    • Docker Start
    • Docker system prune
    • Docker Hosts
    • Docker Logging
    • Docker Save
    • Docker Commands Cheat Sheet
  • Docker Swarm
    • What is Docker Swarm
    • Docker Swarm Architecture
  • Interview Questions
    • Docker Interview Questions

Related Courses

Docker Training Course

Python Training Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Docker Training Course Learn More