How to list running and all Docker containers?
Listing containers is the first command anyone learns and the one most often used. Two flags do almost everything: -a for all states, --filter and --format for everything else.
Theory
TL;DR
docker ps= running containers only.docker ps -a= every container the daemon knows about (running, paused, exited, dead, created).docker ps -q= quiet mode, just container IDs (one per line).- Combine with
--filterfor status/name/label/network/image queries. - Use
--formatfor custom columns or JSON output. Replaces grep/awk pipelines. docker container lsis the long-form alias for the same command.
Quick example
# Default: running containers only
$ docker ps
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
a3f9d2b8c1e4 nginx:1.27-alpine "/docker-entrypoint…" Up 5 minutes 0.0.0.0:8080->80/tcp web
b7e1f4d6a2b8 postgres:16 "docker-entrypoint.s…" Up 5 minutes 5432/tcp db
# All states (running + exited + ...)
$ docker ps -a
CONTAINER ID IMAGE ... STATUS NAMES
a3f9d2b8c1e4 nginx:1.27-alpine ... Up 5 minutes web
b7e1f4d6a2b8 postgres:16 ... Up 5 minutes db
c2d8e3f5a1b7 alpine ... Exited (0) 2 hours ago one-off-test
# IDs only (great for piping)
$ docker ps -aq
a3f9d2b8c1e4
b7e1f4d6a2b8
c2d8e3f5a1b7Useful flag combinations
# Filter by state
docker ps --filter status=running # default
docker ps --filter status=exited # only stopped
docker ps --filter status=paused
# Filter by name (substring match)
docker ps --filter name=web
# Filter by image
docker ps --filter ancestor=nginx:1.27-alpine
# Filter by label
docker ps --filter label=env=prod
# Combine filters (AND)
docker ps -a --filter status=exited --filter name=test
# Show last N created
docker ps -an 5
# Show only the latest one
docker ps -al--format for custom output
# Tab-separated columns
$ docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
NAMES STATUS PORTS
web Up 5 minutes 0.0.0.0:8080->80/tcp
db Up 5 minutes 5432/tcp
# Each container as one JSON line (perfect for jq)
$ docker ps --format json | jq '.Names'
"web"
"db"
# Just names, one per line
$ docker ps --format '{{.Names}}'
web
dbFormat strings let you skip the awk dance.
Common mistakes
Wondering why a container is missing
$ docker ps # empty (or missing your container)
$ docker ps -a # there it is, in Exited stateA crashed or completed container is not in ps's default output. Always check -a when a container disappears.
Using grep when --filter exists
# WRONG: fragile, breaks on substring overlaps
$ docker ps | grep myapp
# RIGHT: structured filter
$ docker ps --filter name=myappThe filter is exact about what it matches; grep is not.
Forgetting -q when piping to delete
# WRONG: passes the entire table including "CONTAINER ID" header
$ docker rm $(docker ps -a)
Error response from daemon: No such container: CONTAINER
# RIGHT: -q gives only IDs
$ docker rm $(docker ps -aq)The canonical "nuke all containers" one-liner is docker rm -f $(docker ps -aq).
Real-world usage
docker ps -aafter a CI build — find the build container's exit code from itsStatusfield.docker ps --filter name=in shell scripts — check if a service is running before doing something.docker ps --format json | jqin monitoring — feed structured data into health dashboards.docker ps -aq | xargs docker rm -fin cleanup hooks — wipe everything between test runs.
Follow-up questions
Q: What is the difference between docker ps and docker container ls?
A: Same command. docker ps is the legacy short form, docker container ls is the modern long form. Identical behavior and flags. Most people still type ps.
Q: How do I see resource usage (CPU, memory) per container?
A: docker stats (live) or docker stats --no-stream (snapshot). Different command than ps; ps lists, stats measures.
Q: Can I see containers from a specific Docker Compose project only?
A: Yes: docker ps --filter label=com.docker.compose.project=<project-name>. Or use docker compose ps from the project directory.
Q: Why does my docker ps output get cut off?
A: Long commands and image names get truncated by default. Use --no-trunc for full strings, or --format to print only the columns you care about.
Q: (Senior) How would you write a one-liner to remove all containers older than a day?
A:
docker ps -a --filter status=exited \
--format '{{.ID}} {{.CreatedAt}}' \
| awk -v d="$(date -d 'yesterday' --iso-8601=seconds)" '$2 < d {print $1}' \
| xargs -r docker rmOr more simply, docker container prune --filter 'until=24h' does the same thing without manual date math.
Examples
Find and tail logs of the most recent container
$ docker logs $(docker ps -alq)
# -a (all states) -l (latest) -q (just ID) → one container ID
# Then docker logs prints its output.Quick way to see what a recently-exited container said before dying.
Cleanup all stopped containers older than an hour
$ docker container prune --filter 'until=1h' -f
Deleted Containers:
c2d8e3f5a1b7...
b9a4f1e2d8c3...
Total reclaimed space: 12.4MBBuilt-in prune saves the manual filter+xargs pipeline for the common case.
Check if a service is up before continuing
# In a shell script
if docker ps --filter name=db --filter status=running -q | grep -q .; then
echo "DB is up"
else
echo "DB not running"
exit 1
fiUseful in deploy scripts and CI checks. The -q plus grep -q . returns true only if at least one container matches.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.
Comments
No comments yet