Sub topic 3
Listing containers with docker ps
This chapter covers docker ps in detail: what each column means, how to see stopped containers, and the flags that narrow or widen the listing. It assumes that Docker is installed and the daemon is running, that you are comfortable at a command line, and that you know the difference between an image and a container. It builds on the container lifecycle (created, running, exited, removed) and on referring to containers by name or ID. Filtering and custom formatting get their own chapter, so this one only previews them.
What docker ps does
docker ps asks the Docker daemon for a list of containers and prints one row per container. By default it shows only containers that are currently running. The name comes from the Unix ps command, which lists processes. A running container is a group of processes, so the comparison fits.
Start a container to have something to list:
docker run -d --name web nginx:alpine
docker ps
docker ps -aThe first command starts an nginx web server in the background (-d means detached) and names it web. The second lists running containers. The third adds -a (short for --all), which includes containers that are not running.
docker ps is a shortcut for docker container ps, and docker container ls does the same thing. All three names are aliases. The docker container form is the newer, grouped syntax. The short form is what you will see in most documentation and scripts.
Reading the default output
A typical listing looks like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f2a9c1b7d44 nginx:alpine "/docker-entrypoint.…" 12 seconds ago Up 11 seconds 80/tcp webEach column answers a different question.
| Column | Meaning |
|---|---|
| CONTAINER ID | A shortened form of the container's 64-character ID. Docker shows the first 12 characters. |
| IMAGE | The image the container was created from, as you gave it to `docker run`. |
| COMMAND | The command the container runs, truncated to fit. |
| CREATED | How long ago the container was created, not when it last started. |
| STATUS | The current state and how long it has been in it. |
| PORTS | Exposed or published ports. |
| NAMES | The container's name. Docker generates one if you do not supply `--name`. |
Some details are easy to misread:
- CREATED and STATUS use different clocks. CREATED counts from when the container was made. STATUS counts from the last start. A container created two days ago and restarted ten minutes ago shows
2 days agoandUp 10 minutes. - COMMAND is truncated. The ellipsis (
…) means the text was cut. Use--no-truncto see all of it. - PORTS distinguishes exposed from published.
80/tcpmeans the image declares port 80 but nothing on the host is mapped to it.0.0.0.0:8080->80/tcpmeans host port 8080 forwards to container port 80. - The ID is a prefix. You can use the 12-character form, or any shorter prefix that is unique, in other commands.
Understanding the STATUS column
STATUS is the column you will rely on most. It begins with a state word and adds timing or an exit code.
| Status text | What it tells you |
|---|---|
| `Up 5 minutes` | The container is running and has been for 5 minutes. |
| `Up 5 minutes (Paused)` | The container's processes are frozen with `docker pause`. |
| `Up 5 minutes (healthy)` | The container defines a health check, and it is passing. |
| `Up 5 minutes (unhealthy)` | The health check is failing, but the container is still running. |
| `Up 2 seconds (health: starting)` | The health check has not yet produced a result. |
| `Exited (0) 3 minutes ago` | The main process finished and returned exit code 0, which means success. |
| `Exited (137) 3 minutes ago` | The main process was killed. 137 is 128 plus signal 9 (SIGKILL). |
| `Created` | The container exists but has never been started. |
| `Restarting (1) 4 seconds ago` | A restart policy is restarting a container that keeps failing. |
| `Dead` | A removal failed partway and left the container in a broken state. |
The number in parentheses after Exited is the exit code of the container's main process. Codes above 128 usually mean a signal ended the process, and the signal number is the code minus 128. So 143 is SIGTERM (15) and 137 is SIGKILL (9). That makes docker ps -a a quick first diagnostic when a container stops unexpectedly.
The diagram shows how these states relate to the commands you use.
docker ps shows containers in Running. docker ps -a shows every state in the diagram except the final one. Once a container is removed it is gone from the listing, because the daemon no longer has a record of it.
Including stopped containers with -a
Without -a, a container that exits disappears from the listing even though it still exists on disk. This confuses many newcomers, who assume the container was deleted. It was not. It is stopped, and its filesystem and metadata are still there.
docker run --name quick alpine echo "done"
docker ps
docker ps -aThe container quick runs echo, prints done, and exits within milliseconds. docker ps shows nothing for it. docker ps -a shows it with a status like Exited (0) 2 seconds ago.
Stopped containers use disk space for their writable layer, and they keep their names. A stopped container named web blocks you from starting a new container with --name web. If a docker run --name fails with a name conflict, docker ps -a is where to look.
Useful flags
docker ps has a small set of flags that cover most daily needs.
| Flag | Long form | Effect |
|---|---|---|
| `-a` | `--all` | Show containers in every state, not only running ones. |
| `-q` | `--quiet` | Print only container IDs, one per line. |
| `-l` | `--latest` | Show only the most recently created container. |
| `-n N` | `--last N` | Show the N most recently created containers, in any state. |
| `-s` | `--size` | Add a SIZE column. |
| `--no-trunc` | Do not truncate the ID, command, or other long fields. | |
| `-f` | `--filter` | Keep only containers matching a condition. |
| `--format` | Print output using a Go template instead of the default table. |
Quiet mode for scripting
-q prints only IDs, which makes the output safe to feed into other commands:
docker ps -q
docker ps -aqThe first prints the IDs of running containers. The second prints the IDs of all containers. Later chapters use this form with docker stop and docker rm to act on many containers at once. It is also the form to use in scripts, because the default table is meant for people and its spacing can change.
The most recent containers
-l and -n are useful right after you start something and need its ID or status:
docker ps -l
docker ps -n 3Both of these include stopped containers, even without -a. This is a known quirk. Passing -n or -l implies that you want recent containers regardless of state.
Seeing disk usage with --size
docker ps -sThe SIZE column has two numbers, such as 1.09kB (virtual 52.5MB). The first is the size of the container's writable layer, meaning the data this container has written on top of its image. The second, "virtual", adds the read-only image layers. Several containers from the same image share those layers, so virtual sizes should not be added together. Computing sizes makes the command slower on hosts with many containers.
Seeing full values
docker ps --no-truncThis prints the full 64-character ID and the full command. Use it when two IDs share the same first 12 characters, which is rare, or when you need the exact command a container was started with.
A first look at filter and format
Two options deserve a short preview, because they change how you use docker ps once you have more than a handful of containers.
docker ps -a --filter "status=exited" --format "table {{.Names}}\t{{.Status}}\t{{.Image}}"--filter "status=exited" keeps only containers that have exited. --format replaces the default columns with the ones you name. Each {{.Field}} is a placeholder from a Go template, and \t puts a tab between columns. The leading word table makes Docker print a header row. Without it, you get raw lines with no header, which is better for scripts.
The next chapter covers the available filters and format fields in full.
Common problems when listing
- A container you just ran is missing. It probably exited. Run
docker ps -a. - The list is empty but you expect containers. You may be talking to a different Docker context or daemon. Check
docker context show. With Docker Desktop and a native Linux install on the same machine, each has its own containers. - Permission denied on the Docker socket. On Linux, your user must belong to the
dockergroup, or you must usesudo. This is a daemon access issue and not specific todocker ps. - Output wraps and is unreadable. Widen the terminal, or use
--formatto select fewer columns. - Names look random. Docker generates names such as
quirky_turingwhen you omit--name. Naming containers makes every later command easier to type.
Summary of key points
docker pslists running containers.docker ps -alists every container that has not been removed.- STATUS combines the current state with timing, health, or an exit code.
- Exit codes above 128 mean a signal ended the process. Subtract 128 to get the signal number.
-qgives IDs only,-nand-lgive the most recent containers in any state, and-sshows writable-layer size.--filterand--formatnarrow and reshape the listing. They are covered in the next chapter.
Exercise
You run docker run --name job alpine sh -c "exit 3" and then docker ps. The output is empty. What command shows the container, what STATUS would you expect, and what does the number mean?
Show answer
Run docker ps -a. The STATUS would read something like Exited (3) a few seconds ago. The container ran, its main process exited, and the 3 is the exit code that the process returned (here set by exit 3). A code of 0 would indicate success, and any non-zero code indicates an error or deliberate failure code.
Exercise
A listing shows Exited (137) 1 minute ago. What most likely happened, and how did you work it out?
Show answer
The main process was killed by SIGKILL (signal 9). Exit codes above 128 mean the process was ended by a signal, and the signal number is the code minus 128, so 137 - 128 = 9. This often happens after docker kill, after docker stop when the grace period expired, or when the kernel killed the process for running out of memory.
Exercise
Which single command prints only the IDs of every container on the host, running or stopped, and why is that output better for scripts than the default table?
Show answer
docker ps -aq. It prints one full-length-prefix ID per line with no header or padding, so another command can consume it directly, for example in $(docker ps -aq). The default table has variable column widths and fields that can contain spaces, which makes it fragile to parse.