Docker commands Guide - docker ps with examples - DevOpsSchool.com (2025)

Here’s a complete tutorial on docker ps, covering how it works, common options, examples, and a list of use cases.

Table of Contents

What is docker ps?

docker ps is a Docker command used to list containers. By default, it shows only the running containers, but with additional options, you can view stopped, exited, or all containers.

Key Features:

  • List all running containers.
  • Filter by status, name, image, and ports.
  • Helps in monitoring and managing containers.
  • Use it to find container IDs for subsequent commands.

Basic Syntax

docker ps [OPTIONS]

Common Options:

  • -a → Show all containers (including stopped and exited ones).
  • -q → Show only container IDs (useful for scripting).
  • -l → Show the last created container (even if stopped).
  • -n <number> → Show n most recent containers.
  • --filter → Filter containers based on criteria (status, name, label, etc.).
  • --format → Format the output using Go templates.
  • --no-trunc → Don’t truncate the container’s output (show full IDs and names).

Examples of docker ps

1. List All Running Containers

docker ps

This shows only the containers that are currently running.

Example Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1b2c3d4e5f6 nginx "nginx -g…" 5 minutes ago Up 5 minutes 80/tcp my_nginx

2. List All Containers (Running, Stopped, and Exited)

docker ps -a

This shows all containers, including those that are stopped or exited.

3. Show Only Container IDs

docker ps -q

Example Output:

a1b2c3d4e5f6b2c3d4e5f678

This is useful in scripts to perform operations on multiple containers.

4. Show the Last Created Container

docker ps -l

Displays the most recently created container, even if it has exited.

5. Show the 3 Most Recently Created Containers

docker ps -n 3

Lists the last three created containers.

6. Filter Containers by Status

docker ps --filter "status=exited"

This shows only containers that have exited.

Other possible status values:

  • running
  • paused
  • restarting
  • exited

7. Filter Containers by Name

docker ps --filter "name=my_nginx"

Lists containers with names that match my_nginx.

8. Filter Containers by Image

docker ps --filter "ancestor=nginx"

Lists all containers created from the nginx image.

9. Display Containers with Specific Ports

docker ps --filter "publish=8080"

Lists containers that expose port 8080 on the host.

10. Show Full Container IDs and Names

docker ps --no-trunc

Prevents truncation, showing full container IDs and names.

11. Format the Output (--format)

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

This customizes the output to show only container ID, name, and status in a table format.

Example Output:

CONTAINER ID NAMES STATUSa1b2c3d4e5f6 my_nginx Up 5 minutes

12. List Containers Created in the Last 24 Hours

docker ps --filter "since=24h"

13. Combine Filters

docker ps --filter "status=exited" --filter "ancestor=nginx"

This lists exited containers created from the nginx image.

14. Use docker ps in a Script to Stop All Running Containers

docker stop $(docker ps -q)

This stops all currently running containers.

15. Use docker ps to Remove All Exited Containers

docker rm $(docker ps -aq --filter "status=exited")

This removes all containers that have exited.

Use Cases for docker ps

1. Monitoring Container Status

  • Check which containers are running and their uptime.
  • Ensure that critical services are active.

2. Managing Resources

  • Find stopped containers and remove them to free up disk space.
  • Identify paused or stuck containers for debugging.

3. Automation and Scripting

  • Combine docker ps with other Docker commands (stop, rm, logs, etc.) to automate maintenance tasks.

4. Debugging and Troubleshooting

  • Filter by exited containers to find failed services.
  • Use --no-trunc to get detailed information about containers for in-depth analysis.

5. Security and Auditing

  • Audit all running containers to ensure they are up-to-date and secure.
  • Monitor containers for unauthorized or unexpected behavior.

6. Multi-Service Applications

  • Quickly check the status of multiple services running in containers.
  • Use docker ps to verify that each service is running on its assigned port.

List of Common docker ps Commands

CommandDescription
docker psList running containers
docker ps -aList all containers
docker ps -qShow only container IDs
docker ps -lShow the last created container
docker ps -n 5Show the last 5 created containers
docker ps --filter "status=running"Show only running containers
docker ps --filter "ancestor=nginx"List containers created from the nginx image
docker ps --no-truncShow full container IDs and names
docker ps --format "table {{.ID}}\t{{.Names}}"Customize the output format

Best Practices for Using docker ps:

  1. Filter and format output for better readability and automation.
  2. Monitor container status regularly to ensure services are healthy.
  3. Use docker ps -q for scripting and batch operations.
  4. Combine with docker logs and docker inspect for detailed troubleshooting.
  5. Clean up unused containers (docker ps -a helps identify them).

Common Errors and Solutions

  1. “No such container”
    → The container may have stopped. Use docker ps -a to find it.
  2. “Error response from daemon: Invalid filter”
    → Ensure the filter is valid. Check available filters with docker ps --help.
  3. Command returns no output
    → There may be no running containers. Use docker ps -a to see stopped ones.

Docker commands Guide - docker ps with examples - DevOpsSchool.com (1)

Rajesh Kumar

I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.

Please find my social handles as below;

Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND

Docker commands Guide - docker ps with examples - DevOpsSchool.com (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6509

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.