Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

Docker commands Guide – docker ps with examples

Hereโ€™s a complete tutorial on docker ps, covering how it works, a comprehensive list of examples, and practical 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

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar 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…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

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