Listing containers

As we continue to run containers over time, we get a lot of them in our system. To find out what is currently-running on our host, we can use the container list command as follows:

$ docker container ls 

This will list all currently-running containers. Such a list might look similar to this:

List of all containers running on the system

By default, Docker outputs seven columns  with the following meanings:


Column


Description

Container ID

The unique ID of the container. It is a SHA-256.

Image

The name of the container image from which this container is instantiated.

Command

The command that is used to run the main process in the container.

Created

The date and time when the container was created.

Status

The status of the container (created, restarting, running, removing, paused, exited, or dead).

Ports

The list of container ports that have been mapped to the host.

Names

The name assigned to this container (multiple names are possible).

If we want to list not only the currently running containers but all containers that are defined on our system, then we can use the command-line parameter -a or --all as follows:

$ docker container ls -a 

This will list containers in any state, such as created, running, or exited.

Sometimes, we want to just list the IDs of all containers. For this, we have the parameter -q:

$ docker container ls -q 

You might wonder where this is useful. I show you a command where it is very helpful right here:

$ docker container rm -f $(docker container ls -a -q)

Lean back and take a deep breath. Then, try to find out what the preceding command does. Don't read any further until you find the answer or give up.

Right: the preceding command deletes all containers that are currently defined on the system, including the stopped ones. The rm command stands for remove, and it will be explained further down.

In the previous section, we used the parameter -l in the list command. Try to use Docker help to find out what the -l parameter stands for. You can invoke help for the list command as follows:

$ docker container ls -h