MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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]
Code language: CSS (css)

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
Code language: JavaScript (javascript)

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"
Code language: JavaScript (javascript)

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"
Code language: JavaScript (javascript)

Lists containers with names that match my_nginx.


8. Filter Containers by Image

docker ps --filter "ancestor=nginx"
Code language: JavaScript (javascript)

Lists all containers created from the nginx image.


9. Display Containers with Specific Ports

docker ps --filter "publish=8080"
Code language: JavaScript (javascript)

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}}"
Code language: JavaScript (javascript)

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"
Code language: JavaScript (javascript)

13. Combine Filters

docker ps --filter "status=exited" --filter "ancestor=nginx"
Code language: JavaScript (javascript)

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)
Code language: JavaScript (javascript)

This stops all currently running containers.


15. Use docker ps to Remove All Exited Containers

docker rm $(docker ps -aq --filter "status=exited")
Code language: JavaScript (javascript)

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.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Top 5 DevOps Tools To Learn Before Writing Your Dissertation

Writing a technical dissertation is rarely just an exercise in reading sources and formatting chapters. On paper, that sounds like the job. In reality, students in computer…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x