Effortless Ways to List Docker Containers

Soumya

Effortless Ways to List Docker Containers.

Docker Containers

Explore More; Quick Start Guide to Docker Compose Logs

Docker is a crucial tool in application development, making it easier to build, deploy, and run applications in containers.

Knowing how to list Docker containers is essential for using Docker effectively. Use the commands docker ps -a, docker container ls, or docker container list to view all running and stopped containers. These commands provide detailed information about each container, including its ID, name, image, ports, and status.

Effortless Ways to List Docker Containers.

1. Listing only running containers:

To list containers that are currently running, use the following basic command:

docker ps

This command is the main way to list all running Docker containers. It displays a table with detailed information about each active container, including its ID, image, command, creation time, status, ports, and name.

Example Output:

CONTAINER ID   IMAGE          COMMAND               CREATED         STATUS                   PORTS       NAMES 

d9b100f2f636   nginx:latest   “nginx -g ‘daemon of…” 2 minutes ago  Up 2 minutes              80/tcp     brave_newton  a1b2c3d4e5f6   ubuntu:latest  “/bin/bash”            10

Here are explanation of detailed information about each running container:

Container ID: The container’s unique ID is identity-mapped to each container, which is used to reference the container in other Docker commands.

Image: It is the name and tag of the Docker image used to create the container.

Command: It is a command run inside the container to start each container’s initialization and running process.

Created: It displays the time when the container was created.

Status: it displays the current status of the container. (“UP” indicates that the container is running, and “Exited (0)” indicates that the container is stopped.)

Ports: It shows details about port mappings between the host and the container.

Names: It shows a unique name assigned to each container.

2. Listing all containers

Use the docker ps command with various options to optimize the output and get more details about Docker containers. The -a flag with docker ps shows all containers, regardless of their state (stopped, running, exited, paused, etc.).

docker ps -a

The output is similar to the docker ps command’s output, but it includes all containers.

From the status section in the output of this command, you can learn about the status of the containers. This command is helpful in identifying containers that have encountered issues and stopped.

3. Listing recently created container

To get information about recently created containers, use the -l option with the docker ps command. This option works like the -a option but also provides more details and lists recently created containers, whether they are running or stopped.

docker ps -l

Recent versions of Docker no longer support this command. Instead, use the docker ps -n N command to list the N most recently created containers. For example, to list the last four created containers, use the following command:

docker ps -n 4

Identifying and checking the most recently created containers is effective for ensuring performance, optimizing resource management for the newest containers, and quick troubleshooting.

4. Listing only container IDs

Sometimes, you may not need detailed information about containers. By using the -q (or --quiet) flag with the docker ps command, you can list only the container IDs.

docker ps -q

This command can be valuable for scripting purposes. Also, listing container IDs is helpful for orchestration tools to manage containers and create connections between containers.

5. Customizing output to display more details

Sometimes, information of the docker ps command’s output is not adequate to use or difficult to understand due to truncation of the value of columns; therefore, for this purpose, you can  disable truncation with the “--no-trunc” option that allows you to get full container names, IDs, and other information:

docker ps –no-trunc

6. Showing container size

Checking the total file size of each container is good practice for optimal resource usage in scenarios where you have limited resources, and you must monitor the resource utilization by each container.

The -s (--size) flag is another useful option to customize Docker container lists by adding an extra column that shows the total file size of each container:

docker ps -s

This command’s output provides information about the actual and virtual disk usage of each container.

The actual size shows the disk usage by the writable container layer, which includes the container’s filesystem and any customizations.

The virtual size reports the total size of all the container’s layers, including the writable layer, shared layers, the image’s read-only layers, and additional layers.

7. Filtering Docker Containers

You can list specific containers based on various criteria such as name, label, ID, status, etc. All you need to do is use --filter flags with key/value field pairs. ‘Key’ will be an attribute of the container, and ‘Value’ will be a condition you want to apply. Here are some common filter options:

Filter by container name, for example:

docker ps –filter “name=my-webserver”

Filter by container ID, for example:

docker ps –filter “id=container_id”

Filter by container state, for example:

docker ps –filter “status=running or created or exited”

Filter by image name, for example:

docker ps –filter “image=ubuntu:latest”

Filter by container label, for example:

docker ps –filter “label=com.example.version=1.0”

Filter by container network, for example:

docker ps –filter “network=bridge”

You can filter containers based on specific volumes, port numbers, and other criteria using the --filter option. This option provides the flexibility to apply advanced filters when listing containers. For example, to list currently stopped containers that publish port 80, use the following command:

docker ps –filter “status=exited” –filter “publish=80”

8. Formatting the Output

You can customize the output of the docker ps command by adding the --format option.

The --format option lets you use Go templating language syntax to get more useful information.

By using the --format option, you can list important features and desired information about the containers instead of the full output, making it easier to manage them. Here’s how it works:

docker ps –format “{{Field1}} {{Field2}}”

This command tailors the container list to the information you require. For example, if you want to get a list of containers with just the ID and status fields, run the following command:

docker ps –format “ID: {{.ID}}, Status: {{.Status}}”

Replace actual values in the command.

In addition, you can create your own table to effectively process container listings, using the following command:

docker ps –format “table {{. Field1 }}\t{{. Field2 }}\t{{. Field3 }}”

9. Listing containers using Docker compose

Docker Compose is the best platform to define multiple containers that work together in your application. So, if you benefit from Docker Compose, run the following command to list associated containers with your current Docker Compose project from a Compose project directory:

docker compose ps

This command, similar to the docker ps command, supports various options to expand its function.

10. Docker ps command alternatives

Two alternative methods to the docker ps command report similar output. Here are other ways to list Docker containers:

docker container list

docker container ls

These commands are useful for your scripts and documentation, but the docker ps command is the most commonly used method due to its short and memorable.

Advantages of Listing Docker Containers

  • Troubleshoot with Ease: Quickly resolve issues by identifying stopped or failed containers.
  • Optimize Resource Usage: Ensure efficient resource management by managing active containers and removing stopped ones.
  • Detailed Container Insights: Retrieve container details effortlessly for various tasks.
  • Monitor Active Ports: View currently used ports with ease by listing Docker containers.
  • Proactive Health Monitoring: Monitor container health checks to promptly detect and address issues.
  • Empower with Scripting: Customize operations by leveraging container listing for your own scripts.

list all docker images

To list all Docker images that are locally available on your system, run the following commands in your terminal:

docker images

Or

docker image ls

List all containers and delete docker

If you want to delete all containers from your Docker, listing the Docker containers is beneficial. To remove all containers, including any currently running ones, first list all Docker containers:

docker ps -a

Therefore, delete all containers by running the following command:

docker rm $(docker ps -a -q)

Before running this command, ensure you do not need any containers because it will remove them.

List unused docker containers

To identify unused Docker containers (stopped containers) there are two methods:

  1. Listing all Docker containers including stopped ones:

docker ps -a

  1. Filtering Docker containers using -f flag and defining container condition:

docker ps -a -f “status=exited”

This command lists all containers with a status of  “exited“.

List all docker IPS

The docker inspect command helps you to list the IP addresses of all Docker containers:

docker inspect -f ‘{{ .NetworkSettings.IPAddress }}’ $(docker

By using the docker ps -q command, you can list all container IDs (quiet mode), and by running docker inspect -f '{{ .NetworkSettings.IPAddress }}' command inspects each container ID, and extracts the IP address.

Explore More; QEMU vs. KVM: Discover the Leaders in Virtualization

Our Plans;GPU Dedicated Server