As a developer working with Docker, being able to efficiently list and manage containers is a crucial skill. Containers encapsulate your applications and services, so you need visibility into what is currently running. This guide will provide an in-depth look at the various options for listing Docker containers.

Docker Container List Commands

The main commands for listing Docker containers are:

  • docker ps – List containers (equivalent to docker container ls)
  • docker container ls – List containers (newer syntax)

By default, these will show running containers. To see all containers, use the -a flag:

docker ps -a
docker container ls -a

Some key parameters and options:

  • -a – Show all containers (default shows just running)
  • -q – Quiet mode. Just show numeric container IDs
  • -s – Display total file sizes
  • –latest , -l – Show the latest created container

Listing All Containers vs Running Containers

Listing only running containers allows you to quickly monitor your active services:

docker ps

Adding -a will list all containers, including stopped ones:

docker ps -a

This helps give you the full picture of existing containers for debugging or cleanup.

Formatting the Output

You can customize the output format using –format:

docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}" 

This renders a table with the ID, Image, and Status columns. You have full control over the formatting.

Some useful formatting options:

  • .ID – Container ID
  • .Names – Container names
  • .Image – Image name
  • .Status – Status (Up, Exited, Created, etc)
  • .Size – Container disk usage
  • .RunningFor – Duration container has been running

Filtering and Finding Specific Containers

You can filter ps output rather than listing all containers:

docker ps -f status=running
docker ps -f name=myapp
docker ps -f label=stage=prod

This skips stopped containers or selects by name/label. Useful for focusing on containers of interest.

Remote Docker Hosts

To list containers on remote hosts, not just local, use -H:

docker -H remote.docker.host:2376 ps

This allows managing containers across clusters and swarm nodes.

Container Lifecycle and Listing

As you build, run, stop and remove containers, what you see from docker ps will change.

Some examples:

  • Stopped containers persist in docker ps -a output until removed
  • Newly created containers show as "Created" if not started
  • Containers rebooting may temporarily show as "Restarting"

So the list output serves as an effective container lifecycle monitor.

Why Listing Containers Matters

Listing gives visibility into running services for monitoring and debugging. Tracking containers via CLI lists or GUIs allows you to:

  • Confirm deployments succeeded and new containers are running
  • Check for unexpected stops or crashes
  • Monitor resource usage (CPU, memory)
  • Clean up unused stopped containers

Overall, being able to accurately list containers is an indispensable Docker admin skill.

Conclusion

Listing containers with docker ps gives you visibility into the status and resource usage of your services. Options like -a, –format, and –filter allow flexible querying to suit different container monitoring needs. Invest time into learning these commands well – it will pay off when tracking and debugging containerized workloads!

Similar Posts