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

Docker ps 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:

  1. List all running containers:

    docker ps
    
  2. List all containers, including stopped ones:

    docker ps -a
    
  3. List the last 5 created containers:

    docker ps -n 5
    
  4. List containers with a specific status (e.g., exited):

    docker ps -a --filter "status=exited"
    
  5. List containers with a specific name:

    docker ps -a --filter "name=my_container"
    
  6. List containers based on a specific image:

    docker ps -a --filter "ancestor=my_image"
    
  7. List container IDs only:

    docker ps -q
    
  8. 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.

  1. How to Use Docker ps Command:

    • Description: The docker ps command is used to list currently running containers.

    • Code Example:

      docker ps
      
  2. Listing Running Containers with Docker ps:

    • Description: docker ps without additional options lists the running containers.

    • Code Example:

      docker ps
      
  3. 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
      
  4. Viewing Container Details with Docker ps:

    • Description: docker ps provides key details like container ID, image, command, status, and ports.

    • Code Example:

      docker ps
      
  5. 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"
      
  6. 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
      
  7. Inspecting Container Processes with Docker ps:

    • Description: docker ps provides a snapshot of the container's main process.

    • Code Example:

      docker ps
      
  8. 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
      
  9. Displaying All Containers with Docker ps -a:

    • Description: Use docker ps -a to show all containers, including stopped ones.

    • Code Example:

      docker ps -a
      
  10. Docker ps and Container Resource Usage:

    • Description: docker ps provides a quick overview of container resource usage.

    • Code Example:

      docker ps
      
  11. Security Considerations with Docker ps:

    • Description: Exercise caution when displaying container information, especially in shared environments.

    • Code Example:

      docker ps
      
  12. 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}}"
      
  13. Docker ps and Multiple Containers:

    • Description: docker ps can list multiple containers at once.

    • Code Example:

      docker ps container1 container2
      
  14. Troubleshooting Issues with Docker ps:

    • Description: Troubleshoot issues with containers using docker ps to identify status and details.

    • Code Example:

      docker ps