iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

docker ps / inspect

docker ps: list containers, filter by state, format output, and the flags that turn a noisy command into useful intel.

Docker — docker ps

EXAMPLE
# ===== Basic =====
docker ps                  # running containers
docker ps -a                # all (including stopped)
docker ps -q                # quiet: just IDs (great for scripts)
docker ps -aq               # all IDs
docker ps -l                # last created (any state)

# ===== Filters =====
docker ps --filter status=running
docker ps --filter status=exited
docker ps --filter name=shop                  # name contains 'shop'
docker ps --filter label=env=prod
docker ps --filter network=app-net
docker ps --filter exited=1                   # exited with code 1
docker ps -a --filter "since=abc123"          # created after another container

# ===== Format =====
docker ps --format "{{.Names}}\t{{.Status}}\t{{.Ports}}"
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"

# All available fields: ID, Image, Command, CreatedAt, RunningFor, Ports, Status, Size, Names, Labels, Mounts, Networks

# JSON for scripting:
docker ps --format json | jq -r '.Names'

# ===== Summary stats =====
docker ps -s                # show size (writable layer + virtual)
docker stats                 # live CPU / RAM / I/O (Ctrl-C to exit)
docker stats --no-stream     # one-shot snapshot

# ===== Useful scripts =====
# Find all stopped containers older than 24h:
docker ps -a --filter status=exited --format '{{.Names}}\t{{.Status}}' | grep -i 'days ago'

# Kill all running containers (rare; nuclear):
docker kill $(docker ps -q)

# Remove all stopped containers:
docker container prune

# ===== Combining with other commands =====
# Inspect the first running container:
docker inspect $(docker ps -q -n 1)

# Logs of all containers named 'shop':
docker ps -q --filter name=shop | xargs -I {} docker logs {}

# ===== Aliases =====
alias dps='docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"'
alias dpsa='dps -a'

# ===== Patterns to internalise =====
# - --format for stable scripting output
# - --filter to scope what you list
# - docker stats for live perf
# - Combine with xargs for batch ops on subsets

# ===== Pitfalls =====
# - 'docker ps' (no -a) missing stopped containers you wanted to find
# - Filter on label without --filter (raw grep is less reliable)
# - 'docker rm $(docker ps -aq)' wipes everything; double-check before running
# - stats --no-stream still costs a small CPU sample window

Why it matters

docker ps with --filter and --format turns container listing into a real tool. Get the regular flags reflexive (-a, -q, -l), use --format for scripts, and pair with docker stats for live perf. Saves a lot of grep + awk pipework.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
docker ps           # running
docker ps -a        # all
docker inspect api
Try it Yourself »

Discussion

Loading…