Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
The docker ps
command is used to list containers, displaying information about their ID, image, command, creation time, status, ports, and names. It is a very useful command for managing and inspecting your containers. In this tutorial, we'll cover the basics of using the docker ps
command.
Syntax:
docker ps [OPTIONS]
OPTIONS
: Additional options that can be used with the docker ps
command.Common options:
-a
or --all
: Show all containers, including stopped ones. By default, only running containers are displayed.--filter
or -f
: Filter the output based on specific conditions, such as status
, name
, label
, or ancestor
.-n
or --last
: Show the last N created containers, including both running and stopped ones.--no-trunc
: Don't truncate output, displaying the full container details.-q
or --quiet
: Only display container IDs.-s
or --size
: Display the total file sizes of containers.Examples:
List all running containers:
docker ps
List all containers, including stopped ones:
docker ps -a
List the last 5 created containers:
docker ps -n 5
List containers with a specific status (e.g., exited):
docker ps -a --filter "status=exited"
List containers with a specific name:
docker ps -a --filter "name=my_container"
List containers based on a specific image:
docker ps -a --filter "ancestor=my_image"
List container IDs only:
docker ps -q
List containers with their total file sizes:
docker ps -s
These examples should give you a basic understanding of how to use the docker ps
command to list and inspect your containers. You can combine different options to tailor the output to your specific needs.
How to Use Docker ps Command:
Description: The docker ps
command is used to list currently running containers.
Code Example:
docker ps
Listing Running Containers with Docker ps:
Description: docker ps
without additional options lists the running containers.
Code Example:
docker ps
Docker ps Command Options and Flags:
Description: Various options and flags exist for customizing the output and behavior of docker ps
.
Code Example:
docker ps -a
Viewing Container Details with Docker ps:
Description: docker ps
provides key details like container ID, image, command, status, and ports.
Code Example:
docker ps
Filtering Containers with Docker ps:
Description: Use filters to narrow down the list of containers based on criteria like name, status, or label.
Code Example:
docker ps --filter "status=running"
Docker ps and Container Names:
Description: docker ps
displays container names, which can be set during container creation.
Code Example:
docker run --name my_container my_image docker ps
Inspecting Container Processes with Docker ps:
Description: docker ps
provides a snapshot of the container's main process.
Code Example:
docker ps
Sorting Output with Docker ps Command:
Description: Sort the docker ps
output based on specific criteria like creation time.
Code Example:
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}" --sort created
Displaying All Containers with Docker ps -a:
Description: Use docker ps -a
to show all containers, including stopped ones.
Code Example:
docker ps -a
Docker ps and Container Resource Usage:
Description: docker ps
provides a quick overview of container resource usage.
Code Example:
docker ps
Security Considerations with Docker ps:
Description: Exercise caution when displaying container information, especially in shared environments.
Code Example:
docker ps
Customizing Output Format with Docker ps:
Description: Customize the output format using the --format
option for docker ps
.
Code Example:
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
Docker ps and Multiple Containers:
Description: docker ps
can list multiple containers at once.
Code Example:
docker ps container1 container2
Troubleshooting Issues with Docker ps:
Description: Troubleshoot issues with containers using docker ps
to identify status and details.
Code Example:
docker ps