---
title: "Mastering Container Inspection: Listing Containers with docker ps and Filters"
date: "2026-11-17T09:00:00+07:00"
description: "Learn how to effectively inspect running and stopped containers using docker ps, custom formatting, and powerful filtering options."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/listing-containers-docker-ps-filters.jpg"
alt: "Docker ps command interface showing container listings"
tags: ['docker', 'containers', 'cli', 'devops', 'sysadmin']
---

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:

```bash
docker ps
```

### Sample Output

```text
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:

```bash
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:

```bash
# 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):

```bash
docker ps -l
```

### 3. Display Size (`-s` or `--size`)
Calculates and displays file sizes consumed by each container:

```bash
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

```bash
# 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

```bash
# 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:

```bash
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:

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

### Filter by Published Port

Find containers publishing a specific host port:

```bash
docker ps --filter "publish=8080"
```

### Filter by Label

Filter containers tagged with metadata labels:

```bash
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:

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

### Common Formatting Placeholders

| Placeholder | Description |
| :--- | :--- |
| `{{.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:

```bash
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

- [Docker Docs: Get started](https://docs.docker.com/get-started/)
- [Docker CLI Reference: docker container ls](https://docs.docker.com/reference/cli/docker/container/ls/)
