Skip to content
Blog

Mastering Container Inspection: Listing Containers with docker ps and Filters

Learn how to effectively inspect running and stopped containers using docker ps, custom formatting, and powerful filtering options.

Published on • November 17, 2026

AI Assistant

When working with Docker, visibility into running and stopped containers is essential for administration, debugging, and system monitoring. The docker ps command (and its alias docker container ls) is your primary gateway for listing containers, inspecting active processes, and filtering metadata.

In this tutorial, we will explore the full capabilities of docker ps, from basic listing to advanced filtering and Go template formatting.


Basic Container Listing

By default, executing docker ps lists only currently running containers:

docker ps

Sample Output

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                  NAMES
a1b2c3d4e5f6   nginx:alpine   "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.080.0.0:80->80/tcp   web-server
7f8e9d0c1b2a   redis:7-alpine "docker-entrypoint.s…"   5 hours ago     Up 5 hours     6379/tcp               cache-db

Key Output Fields

  • CONTAINER ID: The 12-character truncated SHA-256 identifier.
  • IMAGE: The base image name and tag used to run the container.
  • COMMAND: The command executed inside the container upon startup.
  • CREATED: Relative timestamp of when the container was created.
  • STATUS: Current state (e.g., Up 2 hours, Exited (0) 10 minutes ago).
  • PORTS: Network port bindings published to the host machine.
  • NAMES: The assigned or auto-generated name of the container.

Listing All Containers (Including Stopped Ones)

To view all containers on your host machine regardless of state (running, paused, or exited), use the -a or --all flag:

docker ps -a

This reveals stopped containers that may be occupying disk space or retaining network volume references.


Useful Output Flags

1. Quiet Mode (-q)

Returns only the container IDs. This is useful for scripting and chaining commands:

# Stop all running containers
docker stop $(docker ps -q)

# Remove all stopped containers
docker rm $(docker ps -a -q)

2. Display Latest Container (-l or --latest)

Displays only the most recently created container (whether running or stopped):

docker ps -l

3. Display Size (-s or --size)

Calculates and displays file sizes consumed by each container:

docker ps -s

The output includes two size figures:

  • virtual size: Combined disk footprint (read-only base image layers + writable container layer).
  • size: Disk space consumed specifically by the container’s writable layer.

Advanced Container Filtering with --filter

The --filter (or -f) flag enables precise filtering based on key-value criteria.

Filter by Status

# List only exited containers
docker ps --filter "status=exited"

# List only running containers
docker ps --filter "status=running"

# List paused containers
docker ps --filter "status=paused"

Filter by Name

# Exact or partial match on container name
docker ps --filter "name=web-server"
docker ps --filter "name=cache"

Filter by Ancestor Image

List containers created from a specific base image or tag:

docker ps --filter "ancestor=nginx:alpine"
docker ps --filter "ancestor=redis"

Filter by Exit Code

Identify containers that exited with a non-zero exit code:

docker ps -a --filter "exited=1"
docker ps -a --filter "exited=137"

Filter by Published Port

Find containers publishing a specific host port:

docker ps --filter "publish=8080"

Filter by Label

Filter containers tagged with metadata labels:

docker ps --filter "label=environment=production"
docker ps --filter "label=app=backend"

Customizing Output Format with --format

Instead of default columnar table output, use --format with Go template placeholders to extract specific fields:

docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"

Common Formatting Placeholders

PlaceholderDescription
{{.ID}}Container ID (short format)
{{.Names}}Container name(s)
{{.Image}}Image name and tag
{{.Status}}Status string
{{.Ports}}Exposed/published ports
{{.CreatedAt}}Exact creation timestamp
{{.Labels}}All assigned metadata labels

Formatting Output as JSON

For modern CLI automation and script consumption, format output directly as JSON:

docker ps --format "{{json .}}"

Summary

docker ps provides quick visibility into running containers, while flags like -a, -q, -s, --filter, and --format transform it into a powerful diagnostic tool for DevOps engineers and system administrators.

Takeaway: Master docker ps --filter and docker ps --format to streamline container inspection, automate host cleanup tasks, and extract precise metadata in scripts.

Sources