How to list containers in Docker?

In Docker, we can use docker ps to show all running containers, docker ps -a to show all running and stopped containers.

Here are some of the commonly used examples:

Terminal

# common
$ docker ps                             // show only running containers
$ docker ps -a                          // show all containers (running and stopped or exited)

# recent containers
$ docker ps -l                          // To show the latest created container (all states)
$ docker ps -n=5                        // show 5 last created containers (all states)

# file size
$ docker ps -s                          // show the file size of all running containers
$ docker ps -as                         // show the file size of all containers (all states)

# filter examples
$ docker ps -f "status=created"         // show containers which status is `created`
$ docker ps -f "status=exited"          // show containers which status is `exited`, stopped containers.
$ docker ps -af "name=trusting_murdock" // show container which name is 'trusting_murdock'
$ docker ps -af "exited=1"              // show containers which status is ` Exited (1)`, find errors.

# formatting output
$ docker ps -a --format "{{.ID}}: {{.Command}}: {{.Status}}"

# misc
$ docker ps --no-trunc                  // prevent truncating output

What is ps
The ps stands for process status. On Docker, it shows the status of containers along with their IDs.

1. Samples

Here are all the containers.

Terminal

$ docker ps -a

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS                   NAMES        
80a9db188a21   busybox        "sh"                     18 seconds ago Up 17 seconds                                     happy_villani
e90b8831a4b8   nginx          "/bin/bash -c ls"        11 weeks ago   Up 4 hours                                        my_nginx
d362659da5fc   app:1.1        "java -jar app.jar"      9 days ago     Exited (255) 8 days ago    0.0.0.0:80->8080/tcp   xenodochial_volhard
93c591f6b764   eb7e5d08c776   "java -XX:+UnlockCom…"   9 days ago     Exited (1) 9 days ago                             nervous_booth

1.1 Show running containers, Status == Up

Terminal

$ docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS                   NAMES   
80a9db188a21   busybox        "sh"                     18 seconds ago Up 17 seconds                                     happy_villani
e90b8831a4b8   nginx          "/bin/bash -c ls"        11 weeks ago   Up 4 hours                                        my_nginx

1.2 Show 3 last created containers (any status)

Terminal

$ docker ps -n=3                        

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS                   NAMES        
80a9db188a21   busybox        "sh"                     18 seconds ago Up 17 seconds                                     happy_villani
e90b8831a4b8   nginx          "/bin/bash -c ls"        11 weeks ago   Up 4 hours                                        my_nginx
d362659da5fc   app:1.1        "java -jar app.jar"      9 days ago     Exited (255) 8 days ago    0.0.0.0:80->8080/tcp   xenodochial_volhard

1.3 Show file size of running containers.

Terminal

$ docker ps -s

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS                   NAMES             SIZE
80a9db188a21   busybox        "sh"                     18 seconds ago Up 17 seconds                                     happy_villani     0B (virtual 1.22MB)
e90b8831a4b8   nginx          "/bin/bash -c ls"        11 weeks ago   Up 4 hours                                        my_nginx          58.48 kB (virtual 99.2 MB)

1.4 Filter by status.

Terminal

$ docker ps -f "status=exited"

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS                   NAMES        
d362659da5fc   app:1.1        "java -jar app.jar"      9 days ago     Exited (255) 8 days ago    0.0.0.0:80->8080/tcp   xenodochial_volhard
93c591f6b764   eb7e5d08c776   "java -XX:+UnlockCom…"   9 days ago     Exited (1) 9 days ago                             nervous_booth


$ docker ps -af "exited=1"   

93c591f6b764   eb7e5d08c776   "java -XX:+UnlockCom…"   9 days ago     Exited (1) 9 days ago                             nervous_booth

1.5 Filter by container id

Terminal

$ docker ps -af id=d362659da5fc

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS                   NAMES        
d362659da5fc   app:1.1        "java -jar app.jar"      9 days ago     Exited (255) 8 days ago    0.0.0.0:80->8080/tcp   xenodochial_volhard

Further Reading
Please refer to this official Docker ps guide for more supported filters.

1.6 Formatting output

Terminal

$ docker ps -a --format "{{.ID}}: {{.Command}}: {{.Status}}"


80a9db188a21: "sh": Up 17 seconds
e90b8831a4b8: "/bin/bash -c ls": Up 4 hours
d362659da5fc: "java -jar app.jar": Exited (255) 8 days ago
93c591f6b764: "java -XX:+UnlockCom…": Exited (1) 9 days ago

2. docker ps –help

This is the most powerful command.


$ docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all
                        states) (default -1)
  -l, --latest          Show the latest created container (includes all
                        states)
      --no-trunc        Dont truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar