Docker ps command
The docker ps command is used to list Docker containers.
By default, the docker ps command only shows running containers, but you can also specify options to show all containers, including stopped ones.
Syntax
docker ps [OPTIONS]
OPTIONS description:
-a, --allShow all containers, including stopped containers.-q, --quiet: Only show container IDs.-l, --latestShow the most recently created container, including all states.-nShow the n most recently created containers, including all states.--no-trunc: Do not truncate output.-s, --size: Show container sizes.--filter, -f: Filter displayed containers based on conditions.--format: Format the output.
Example
1. List information about all running containers
By default, docker ps only shows running containers.
docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 09b93464c2f7 nginx:latest "nginx -g 'daemon off" ... 80/tcp, 443/tcp myexample 96f7f14e99ab mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
Output detailed introduction:
CONTAINER ID:Container ID.
IMAGE:The image used.
COMMAND:The command run when starting the container.
CREATED:The creation time of the container.
STATUS:Container status.
There are 7 states:
- created (created)
- restarting (restarting)
- running (running)
- removing (migrating)
- paused
- exited
- dead
PORTS:Container port information and the connection type used (tcp/udp).
NAMES:The automatically assigned container name.
2. List all containers, including stopped containers.
docker ps -a
Show all containers, including stopped containers.
3. Only show container IDs.
docker ps -q
Only show container IDs.
4. Show the most recently created container.
docker ps -l
Show the most recently created container, including all states.
5. Show the n most recently created containers.
docker ps -n 3
Show the 3 most recently created containers, including all states.
6. Show container sizes.
docker ps -s
Show container sizes.
7. Filter displayed containers based on conditions.
docker ps -f "status=exited"
Show containers with a status of exited.
docker ps -f "name=my_container"
Show containers with names containing my_container.
8. Formatted output
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"Display the container ID, name, and status in a table format.
Common filters
status: Container status (such asrunning、paused、exited)。name: Container name.id: Container ID.label: Container label.ancestor: Container image.
Use Case
- Monitor container status: Monitor the status and resource usage of running containers in real time.
- Debugging and managementView all containers, including stopped containers, for debugging and management operations.
- Automation scriptsUse filters and formatting options to easily retrieve specific container information in automation scripts.
other extensions