Sub topic 4
Filtering and Formatting the Output
This chapter assumes you can run docker ps and docker ps -a, and that you know the difference between an image and a container. By the end, you will be able to narrow a container listing to the rows you want and shape the columns to fit your task.
Why filter and format
On a busy host, docker ps -a prints dozens of rows and a wide table. Two flags fix this:
--filter(short form-f) keeps only the containers that match a condition.--formatchooses which fields to print and how.
Both flags matter for scripts, because they make the output predictable.
Filtering with --filter
A filter has the form key=value. The most common keys are:
| Key | Matches | Example |
|---|---|---|
| `status` | Container state | `status=exited` |
| `name` | Part of the container name | `name=web` |
| `ancestor` | Containers created from an image | `ancestor=nginx:alpine` |
| `label` | A label key or key=value | `label=env=prod` |
| `exited` | Exit code (use with `-a`) | `exited=1` |
Valid status values are created, restarting, running, removing, paused, exited, and dead.
Start a container, then list only the exited ones:
docker run -d --name web nginx:alpine
docker ps -a --filter "status=exited"Without -a, docker ps shows only running containers. A filter such as status=exited therefore returns nothing unless you also pass -a.
Combining filters
Repeat --filter to combine conditions. Different keys are joined with AND. Repeating the same key is joined with OR.
docker ps -a --filter "status=exited" --filter "ancestor=nginx:alpine"
docker ps -a --filter "status=exited" --filter "status=created"The first command lists exited containers built from nginx:alpine. The second lists containers that are either exited or created.
Name filters match substrings
--filter "name=web" also matches web2 and my-web-app. To match an exact name, anchor it with a regular expression:
docker ps -a --filter "name=^web$"Formatting with --format
--format takes a Go template. Fields go inside double braces, such as {{.Names}}. Common fields are .ID, .Names, .Image, .Status, .State, .Ports, .Command, and .CreatedAt.
docker ps -a --format "{{.Names}}: {{.Status}}"This prints one line per container, for example web: Up 2 minutes.
Printing a table
Start the template with the word table to get a header row and aligned columns. Use \t to separate columns.
docker ps -a --filter "status=exited" --format "table {{.Names}}\t{{.Status}}\t{{.Image}}"The result lists only exited containers, with three columns and a header line.
Printing IDs only
The -q (quiet) flag prints only container IDs. It pairs well with --filter and is the basis for bulk operations later in this tutorial.
docker ps -aq --filter "status=exited"--format "{{.ID}}" produces similar output, but -q is shorter.
How the pieces fit together
Docker applies the filter first, then formats whatever remains.
Common mistakes
- Forgetting
-awhen filtering for stopped containers. - Expecting
name=webto match onlyweb. - Putting spaces around the
=in a filter. Writestatus=exited, notstatus = exited. - Using
\tin a template but wrapping it in single quotes in PowerShell, where quoting rules differ. Use double quotes, as shown above.
Summary
--filter key=valuenarrows the rows. Different keys combine with AND, and the same key repeated combines with OR.--formatwith a Go template controls the columns. Prefix it withtablefor headers.-qprints only IDs, ready for use in other commands.
Exercise
Write one command that lists all containers, running or stopped, whose state is exited and whose name contains "web". Show only the name and status, in a table with a header.
Show answer
docker ps -a --filter "status=exited" --filter "name=web" --format "table {{.Names}}\t{{.Status}}"
Exercise
You run docker ps --filter "status=exited" and get no rows, even though you know a container exited a minute ago. What is the most likely cause, and what is the fix?
Show answer
Without -a, docker ps lists only running containers, so the exited one is never considered. Add -a: docker ps -a --filter "status=exited".