Command To List Docker Containers

Command To List Docker Containers

Reading time1 min
#Docker#Containers#DevOps#containerlist#dockercommands#dockerps

Mastering Docker Container Visibility: Beyond the Basic docker ps Command

Why settle for docker ps when the real container insights lie in less obvious commands and filtering techniques? This post challenges the default approach, unveiling advanced listing commands that reveal more about your containers than you thought possible.


If you're a developer or system administrator working with Docker, you’re probably very familiar with the docker ps command. It’s the go-to tool for listing running containers and quickly checking their IDs, names, statuses, and ports. But if you stop there, you’re missing out on powerful commands and options that provide a much deeper understanding of your container environment.

Why Go Beyond docker ps?

The basic docker ps command shows running containers, but what if you want to:

  • See all containers including stopped ones?
  • Filter containers based on specific criteria?
  • Display detailed information such as size or resource usage?
  • Combine container status with network or volume data?

Mastering more advanced Docker container listing techniques helps you monitor, debug, and optimize your environments much more efficiently.


The Basics Recap: docker ps

The simplest form of this command:

docker ps

By default, it shows all running containers:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                    NAMES
f3db7a150c7a   nginx:latest   "nginx -g 'daemon of…"   15 hours ago    Up 15 hours    0.0.0.0:80->80/tcp       webserver
d2b6fcccd8b6   redis:alpine   "redis-server"           3 days ago      Up 3 days     6379/tcp                 redis-cache

But what if a container has exited? You won’t see it here.


Show All Containers (Running & Stopped)

To see all containers regardless of their state:

docker ps -a

Output example:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                      PORTS                    NAMES
f3db7a150c7a   nginx:latest   "nginx -g 'daemon of…"   15 hours ago    Up 15 hours                 0.0.0.0:80->80/tcp       webserver
d2b6fcccd8b6   redis:alpine   "redis-server"           3 days ago      Exited (0) 10 hours ago                               redis-cache

This is useful for debugging stopped containers or checking history.


Filter Containers by Status

What if you only want to see containers that are currently exited or paused? Use the --filter (or -f) option with status qualifiers:

docker ps -a --filter "status=exited"

This will list all containers that are stopped or exited.

Similarly:

docker ps --filter "status=running"

Lists only running containers (similar to default behavior).

More Filter Options

Other useful filters include:

  • name= filter to search by partial name:

    docker ps -a --filter "name=web"
    
  • ancestor= filter to find containers from a specific image:

    docker ps --filter "ancestor=nginx:latest"
    
  • id= filter for listing specific container IDs.


Display Size Information

Want to know how much disk space each container is consuming?

docker ps -s

This adds two columns: SIZE and VIRTUAL SIZE, indicating writable layer size and total image virtual size respectively.


Customize Output Format

Customize what fields are shown using the --format flag, which accepts Go-style templates. This is great for scripting or concise output.

Example: Show only container names and statuses

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

Output:

NAMES           STATUS
webserver       Up 15 hours
redis-cache     Exited (0) 10 hours ago

Other Available Fields

Some common formatting fields include:

  • .ID
  • .Image
  • .Command
  • .CreatedAt
  • .RunningFor
  • .Ports
  • .Status
  • .Names

You can find a comprehensive list in Docker’s official documentation or by experimentation.


Combine Filtering and Formatting

You can combine filters with formats for precise control. For example, show all exited containers with just their ID and exit code:

docker ps -a --filter "status=exited" --format "{{.ID}}\t{{.Status}}"

Inspect Container Resource Usage Quickly

While not part of docker ps, tools like docker stats show live resource metrics per container which complements visibility for performance tuning.

Example use alongside filtering:

docker stats $(docker ps -q --filter "ancestor=nginx")

Here we list the container IDs for all running nginx instances and pass them to docker stats.


Recap & Practical Tips

Use CaseCommand Example
List running containersdocker ps
List all containersdocker ps -a
List exited containersdocker ps -a --filter "status=exited"
List containers matching namedocker ps -a --filter "name=app"
List with disk usage infodocker ps -s
Custom output formatdocker ps --format "{{.Names}} {{.Status}}"

If you’re regularly troubleshooting or managing complex multi-container setups, integrating these commands into your workflow can save time and improve insight significantly.


Final Thought

Don’t let the simplicity of docker ps fool you — it’s just the surface of what Docker offers when it comes to inspecting your containers. Advanced filtering options, formatting capabilities, and combining multiple commands together can unlock a clearer picture into your containerized world.

Go beyond the basics today — master these Docker listing commands and watch your operational visibility soar!


Happy Dockering! 🚢🐳