Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker ps with examples

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


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       NAMES
a1b2c3d4e5f6   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:

a1b2c3d4e5f6
b2c3d4e5f678

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       STATUS
a1b2c3d4e5f6   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.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x