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 specify options to show all containers, including stopped ones.
Syntax
docker ps [OPTIONS]
OPTIONS description:
-a, --all: Show all containers, including stopped ones.-q, --quiet: Show only container IDs.-l, --latest: Show the most recently created container, including all states.-n: Show the n most recently created containers, including all states.--no-trunc: Do not truncate the output.-s, --size: Show container sizes.--filter, -f: Filter the displayed containers based on conditions.--format: Format the output.
Example
1. List information of 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 myrunoob
96f7f14e99ab mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
Output details:
CONTAINER ID: Container ID.
IMAGE: The image used.
COMMAND: The command run when the container was started.
CREATED: The container's creation time.
STATUS: Container status.
There are 7 statuses:
- created (created)
- restarting (restarting)
- running (in operation)
- removing (removing)
- paused (paused)
- exited (stopped)
- dead (dead)
PORTS: The container's port information and the connection type used (tcp/udp)
NAMES: The automatically assigned container name.
2. List all containers, including stopped ones
docker ps -a
Show all containers, including stopped ones.
3. Show only container IDs
docker ps -q
Show only container IDs.
** 4. Display the most recently created container**
docker ps -l
Display the most recently created container, including all states.
** 5. Display the n most recently created containers**
docker ps -n 3
Display the 3 most recently created containers, including all states.
6. Show container sizes
docker ps -s
Show the size of containers.
** 7. Filter displayed containers by conditions**
docker ps -f "status=exited"
Show containers whose status is exited.
docker ps -f "name=my_container"
Show containers whose names contain my_container.
** 8. Format the output**
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
Display container IDs, names, and statuses in tabular form.
Common filters
status: Container status (such asrunning、paused、exited)。name: Container name.id: Container ID.label: Container label.ancestor: Container image.
Use Cases
- Monitor container status: Monitor the status and resource usage of running containers in real time.
- Debugging and management: View all containers, including stopped ones, for debugging and management operations.
- Automation scripts: Use filters and formatting options to easily obtain specific container information in automation scripts.
Docker Command Reference