965

There's a command to list images, docker images, but there doesn't seem to be a corresponding docker containers.

Other than becoming root and looking into /var/lib/docker there doesn't seem a way to do that. Am I missing something? Is that something one isn't supposed to do?

1

14 Answers 14

1852

To show only running containers use the given command:

docker ps

To show all containers use the given command:

docker ps -a

To show the latest created container (includes all states) use the given command:

docker ps -l

To show n last created containers (includes all states) use the given command:

docker ps -n=-1

To display total file sizes use the given command:

docker ps -s

The content presented above is from docker.com.

In the new version of Docker, commands are updated, and some management commands are added:

docker container ls

It is used to list all the running containers.

docker container ls -a

And then, if you want to clean them all,

docker rm $(docker ps -aq)

It is used to list all the containers created irrespective of its state.

And to stop all the Docker containers (force)

docker rm -f $(docker ps -a -q)  

Here the container is the management command.

5
  • @RutgerHofste both commands (docker rm and docker ps) do show up in docker's help.
    – Munchkin
    Oct 16, 2017 at 12:28
  • 20
    Another helpful way to clean all unnecessary items like dangling containers, unused images, logs etc is by using docker system prune --all. You can find the docker documentation here Oct 19, 2017 at 14:42
  • after an image is pulled, ps -a, does NOT list it. seems you can only list images that have run at least once?
    – ftravers
    Dec 26, 2017 at 5:49
  • docker ps lists all running and paused containers. To list only the running ones: docker ps --filter="status=running" Mar 21, 2018 at 15:31
  • @ftravers docker ps only shows information about containers. What you want is information about images. You can see all local images using the docker images command.
    – Paul
    Nov 5, 2019 at 9:36
154

To list all running and stopped containers

docker ps -a

To list all running containers (just stating the obvious and also example use of -f filtering option)

docker ps -a -f status=running

To list all running and stopped containers, showing only their container id

docker ps -aq

To remove all containers that are NOT running

docker rm `docker ps -aq -f status=exited`
32

Note that some time ago there was an update to this command. It will not show the container size by default (since this is rather expensive for many running containers). Use docker ps -s to display container size as well.

1
  • As per mrh's answer below, you will also need the -a switch to view containers that are not running (likely to be the case for those you'd like to remove), so the final command is docker ps -as (don't forget to prepend sudo if the daemon is running as root) Apr 19, 2016 at 23:56
21

docker ps -s will show the size of running containers only.

To check the size of all containers use docker ps -as

20

There are also the following options:

docker container ls
docker container ls -a
# --all, -a
# Show all containers (default shows just running)

since: 1.13.0 (2017-01-18):

Restructure CLI commands by adding docker image and docker container commands for more consistency #26025

and as stated here: Introducing Docker 1.13, users are encouraged to adopt the new syntax:

CLI restructured

In Docker 1.13, we regrouped every command to sit under the logical object it’s interacting with. For example list and start of containers are now subcommands of docker container and history is a subcommand of docker image.

These changes let us clean up the Docker CLI syntax, improve help text and make Docker simpler to use. The old command syntax is still supported, but we encourage everybody to adopt the new syntax.

1
  • 1
    great info, a shame that docker --help has the suggested interface 3 pages back in its output, hidden before the deprecated commands!
    – danio
    Mar 8, 2018 at 10:07
18

The Docker command set is simple and holds together well:

docker stack ls
docker service ls
docker image ls
docker container ls

Teaching the aliases first is confusing. Once you understand what's going on, they can save some keystrokes:

docker images -> docker image ls
docker ps -> docker container ls
docker rmi -> docker image rm
docker rm -> docker container rm

There are several aliases in Docker. For instance:

docker rmi
docker image rm
docker image rmi
docker image remove

are all the same command (see for your self using docker help image rm).

15

To list only the containers SHA1:

docker ps -aq --no-trunc

That way, you can use the list of all containers for other commands (which accept multiple container ids as parameters).

For example, to list only the name of all containers (since docker ps list only their names with other information):

docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc)
7

There are many ways to list all containers.

You can find using 3 Aliasesls, ps, list like this.

sudo docker container ls 
sudo docker container ps
sudo docker container list
sudo docker ps
sudo docker ps -a

You can also use give option[option].

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 last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes

You can use an option like this:

sudo docker ps //Showing only running containers
sudo docker ps -a //All container (running + stopped)
sudo docker pa -l // latest
sudo docker ps -n <int valuse 1,2,3 etc>// latest number of created containers
sudo docker ps -s // Display container with size
sudo docker ps -q // Only display numeric IDs for containers
docker docker ps -a | tail -n 1 //oldest container
1
  • To show the docker container list command it in tidy output: docker container list --format 'table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.RunningFor}}\t{{.Status}}\t{{.Networks}}\t{{.Ports}}' | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}' | awk 'BEGIN{FS=OFS="[[:space:]][[:space:]]+"} gsub("0.0.0.0:", "", $7) gsub(",?:::[^,]+", "", $7) gsub(" , "," ",$7) sub(", $", "", $7) sub(".*/.*/", "", $3) sub("minutes","m",$4) sub("minutes","m",$5) sub("hours","h",$4) sub("hours","h",$5) sub("ago","",$4) {printf("%-15s%-22s%-38s%-16s%-22s%-21s%s\n",$1" ",$2" ",$3" ",$4" ",$5" ",$6" ",$7)}' Jun 8, 2023 at 15:09
5

To display only running containers

docker ps

To show all containers (includes all states)

docker ps -a

To show the latest created container (includes all states)

docker ps -l

To show n last created containers (includes all states)

docker ps -n=-1

To display total file sizes

docker ps -s

In the new version of Docker, commands are updated, and some management commands are added:

docker container ls

List all the running containers.

docker container ls -a

5

List running containers:-

$ docker ps

List all containers:-

$ docker ps -a

List only stopped containers:-

$ docker ps --filter "status=exited"

or

$ docker ps -f "status=exited"

2
  • 2
    These commands have already been posted multiple times - what's the reason for duplicating other answers?
    – Nico Haase
    Apr 9, 2020 at 8:28
  • @NicoHaase Probably to collect upvotes. 4 people have upvoted this answer already, which, as you rightly point out, has not contributed anything new. You, however, get a +1 from me for your comment :-) Oct 12, 2020 at 10:13
4

Use docker container ls to list all running containers.

Use the flag -a to show all containers (not just running). i.e. docker container ls -a

Use the flag -q to show containers and their numeric IDs. i.e. docker container ls -q

Visit the documentation to learn all available options for this command.

3

just a convenient way of getting last n=5 containers (no matter running or not):

$ docker container ls -a -n5
2

I got the error message Cannot connect to the Docker daemon. I forgot I am running the daemon as root and needed sudo:

$ sudo docker ps
1
  • 1
    alias docker="sudo --group docker docker" Feb 13, 2017 at 18:15
1
docker ps [OPTIONS]

Following command will show only running containers by default.

docker ps

To see all containers:

docker ps -a

For showing the latest created container:

docker ps -l

Not the answer you're looking for? Browse other questions tagged or ask your own question.