docker ps
The docker ps command is used to list Docker containers. By default, it shows only running containers. Here is an in-depth explanation of docker ps along with examples and relevant switches and parameters.
Basic Syntax
Key Options and Parameters
-a, --all: Show all containers (default shows just running).
docker ps -a-q, --quiet: Only display numeric IDs.
docker ps -q--filter: Filter output based on conditions provided.
docker ps --filter "status=exited"--format: Pretty-print containers using a Go template.
docker ps --format "{{.ID}}: {{.Names}}"-n, --last: Show n last created containers (includes all states).
docker ps -n 5-s, --size: Display total file sizes.
docker ps -s
Examples
List Running Containers
docker psThis command lists all currently running containers.
List All Containers
docker ps -aThis command lists all containers, including those that are stopped.
List Only Container IDs
docker ps -qThis command lists only the IDs of all running containers.
Filter Containers by Status
docker ps --filter "status=exited"This command lists only the containers that have exited.
Format Output
docker ps --format "{{.ID}}: {{.Names}}"This command formats the output to show only the container ID and name.
Show Last 5 Created Containers
docker ps -n 5This command lists the last 5 created containers, including all states.
Display Total File Sizes
docker ps -sThis command lists running containers and displays their total file sizes.
Conclusion
The docker ps command is essential for managing and monitoring Docker containers. Understanding its options and parameters allows you to effectively list and filter containers based on your needs.