Learn Docker:Fundamentals of Docker 19.x
上QQ阅读APP看书,第一时间看更新

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 ls 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           This is the unique ID of the container. It is an SHA-256.
Image           This is the name of the container image from which this container is instantiated.
Command           This is the command that is used to run the main process in the container.
Created           This is the date and time when the container was created.
Status           This is the status of the container (created, restarting, running, removing, paused, exited, or dead).
Ports           This is the list of container ports that have been mapped to the host.
Names           This is 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 --allas 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 -q parameter:

$ docker container ls -q

You might wonder where this is useful. I will 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.

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 soon.

In the previous section, we used the -l parameter 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

Next, let's learn how to stop and restart containers.