docker
bash
auto-refresh
system-monitoring
command-line

Trick to loop/autorefresh docker ps view like top/htop in bash

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

docker ps is a snapshot command, not an interactive monitor. If you want a live view that behaves more like top or htop, the usual approach is to rerun docker ps on a timer with watch or a small Bash loop and keep the output narrow enough to scan quickly.

The Fastest Option: watch

On most Linux systems, watch is the easiest tool for this job:

bash
watch -n 2 docker ps

That reruns docker ps every two seconds. It is a good default when you are waiting for a container to start, restart, or disappear.

The default docker ps output is often wider than the terminal, so using --format makes the live view much cleaner:

bash
watch -n 2 "docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'"

This reduces noise and keeps the important columns stable while the screen refreshes.

Using A Bash Loop For More Control

If watch is missing or you want to customize the screen, a Bash loop gives you more control:

bash
1while true; do
2  clear
3  docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
4  sleep 2
5done

This behaves like a minimal dashboard. The clear call erases the previous frame, docker ps prints the current container state, and sleep 2 controls the refresh interval. Exit with Ctrl+C.

One advantage of a loop is that you can add your own context. A timestamp is often the first useful addition:

bash
1while true; do
2  clear
3  date
4  echo
5  docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
6  sleep 2
7done

That makes it easier to match what you see on screen with logs, deploy steps, or health-check failures.

Filtering The Output

On a busy host, a full docker ps table becomes hard to read. Use filters and narrower columns to focus on a specific service:

bash
watch -n 2 "docker ps --filter name=web --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'"

If you are debugging crash loops, include exited containers as well:

bash
watch -n 2 "docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'"

That is important because containers that fail immediately may not stay in the running list long enough to notice.

Wrapping It In A Shell Function

If you do this often, a shell function saves time and keeps the quoting in one place:

bash
1dpswatch() {
2  local interval="${1:-2}"
3  watch -n "$interval" "docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'"
4}

Now you can run:

bash
dpswatch

Or choose a different refresh interval:

bash
dpswatch 1

You can add other flags, such as a name filter or -a, once you see what you use repeatedly.

When docker ps Is Not Enough

docker ps tells you which containers exist and whether Docker considers them up, restarting, or exited. It does not give live CPU and memory usage. If your real goal is resource monitoring, use docker stats:

bash
docker stats

That command is much closer to top semantics because it streams usage data continuously. A common workflow is to watch docker ps for state changes and switch to docker stats when you need to see whether a container is CPU-bound or memory-constrained.

Another useful variation is to pair a live docker ps view with logs in a second terminal:

bash
docker logs -f web

That gives you a state view in one pane and the application output in another.

Quoting And Portability

Docker's --format option uses Go-template placeholders such as {{.Names}}. The shell also interprets quotes and special characters, so command quoting matters. A reliable pattern is:

  • wrap the whole watch command in double quotes
  • use single quotes inside the Docker format string
  • keep the format short enough to fit the terminal

If the quoting is wrong, watch may run but Docker may receive a broken template. That usually shows up as missing columns or template parse errors.

Common Pitfalls

  • Refreshing too quickly and creating a flickering screen that is harder to read than a static command.
  • Forgetting that docker ps only shows running containers unless you add -a.
  • Watching too many columns at once and making status changes easy to miss.
  • Using docker ps when the real question is CPU or memory usage, which belongs to docker stats.
  • Misquoting the Go-template placeholders inside a watch command.

Summary

  • 'watch -n 2 docker ps is the quickest way to get an auto-refreshing container view.'
  • A Bash loop is better when you want timestamps, custom layout, or extra logic.
  • '--format keeps the display compact and readable.'
  • 'docker ps -a helps when containers exit too fast to observe.'
  • 'docker stats is the right tool when you want a live resource dashboard instead of state snapshots.'

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.