Sub topic 3
Listing containers with docker ps
This chapter assumes Docker is installed and the daemon is running. It also assumes you can use a shell and know that an image is a template and a container is a running or stopped instance of it. Container states and names were covered earlier. Filtering and formatting come in the next chapter.
What docker ps shows
docker ps asks the Docker daemon for a list of containers and prints one row per container. By default it shows only running containers. Adding -a (short for --all) includes containers in every state, such as created, exited, and paused.
Start a container to have something to list:
docker run -d --name web nginx:alpine
docker ps
docker ps -aThe -d flag runs the container detached, so it stays in the background and your terminal is free. The first docker ps shows web. The second shows the same container, plus any stopped containers left over from earlier work.
Reading the columns
The default output has seven columns.
| Column | Meaning |
|---|---|
| CONTAINER ID | A shortened form of the container's unique ID (12 characters). |
| IMAGE | The image the container was created from. |
| COMMAND | The command the container runs, truncated to fit. |
| CREATED | How long ago the container was created. |
| STATUS | The current state and how long it has been in that state. |
| PORTS | Published ports and the host addresses they map to. |
| NAMES | The container's name. |
Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f2a9c1b7d4e nginx:alpine "/docker-entrypoint.…" 12 seconds ago Up 11 seconds 80/tcp webSTATUS is the column you will check most often. Typical values are:
Up 11 seconds: the container is running.Up 2 hours (Paused): the container is running but its processes are frozen.Exited (0) 5 minutes ago: the container stopped. The number is the exit code.0means a clean exit, and anything else usually means an error or a signal.Created: the container exists but has never been started.
Useful flags
You can combine these flags with each other.
| Flag | Effect |
|---|---|
| `-a`, `--all` | Show containers in all states, not just running ones. |
| `-q`, `--quiet` | Print only container IDs, one per line. |
| `-l`, `--latest` | Show only the most recently created container. |
| `-n N` | Show the last N created containers. |
| `-s`, `--size` | Add a SIZE column with the writable-layer size. |
| `--no-trunc` | Do not truncate the ID or the command. |
For example, docker ps -aq prints the IDs of every container. This is what makes the bulk commands in a later chapter possible, because the output can be passed straight to another command.
docker ps -aq
docker ps -n 3
docker ps -asThe -s flag can be slow on hosts with many containers, because Docker has to compute the size of each one.
Reading the PORTS column
An entry such as 0.0.0.0:8080->80/tcp means that port 8080 on all host interfaces forwards to port 80 inside the container. An entry such as 80/tcp with no arrow means the image declares that port, but nothing is published to the host. You cannot reach it from outside the container's network until you publish it with -p.
How the lifecycle shows up
Running docker ps and docker ps -a side by side tells you where a container is in its lifecycle. A container that appears in both lists is running. A container that appears only with -a is created or stopped. A container that appears in neither has been removed.
The diagram below summarizes which command shows which containers.
Common mistakes
Two other errors come up often:
- Running
docker psas a user without permission to reach the Docker socket fails with a "permission denied" error. Add your user to thedockergroup or usesudo. - Reading
CREATEDas "when it started". It is the creation time. A container that was stopped and started again keeps its original creation time, while STATUS reflects the most recent start.
Exercise
You ran docker run -d --name web nginx:alpine, and later docker ps shows nothing, but you did not remove the container. Which command shows it, and what in the output tells you whether it exited cleanly?
Show answer
Run docker ps -a. The STATUS column will read something like Exited (0) 5 minutes ago. An exit code of 0 means a clean exit. A non-zero code means the main process failed or was killed.
Exercise
Which single command prints only the IDs of all containers in every state?
Show answer
docker ps -aq. The -a includes stopped containers and -q limits the output to IDs.