List Docker Containers Easily with Commands

Listing Docker containers involves using commands like docker ps and docker ps -a to view running and all containers respectively, showing key details like container ID, image, and status.

🤖AI Overview:

Listing Docker containers is an essential Docker management task that helps users view all running and stopped containers. Using commands such as docker ps and docker ps -a, users can access detailed container information. Advanced options like filtering and custom formatting enhance management and monitoring capabilities.

Learning how to list Docker containers is an important skill when using Docker. You can list all running and stopped containers with details about container ID, name, image, ports, and status using the docker ps -a, docker container ls, or docker container list commands.

Comprehensive guide to list Docker containers

Working with Docker brings valuable advantages for developers; if you have not installed Docker on your server yet, by referring to our knowledge base, you can learn how to install Docker on a Linux server and configure Docker on a Windows server. Also, you need to buy VPS to optimize your Docker environments and usage.

While using Docker, listing Docker containers is a frequent task that you should accomplish in managing Docker containers to manage your workload optimally. Here are the most common methods 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 primary command to list all running docker containers, and it will display a table with detailed information about each active container, including container 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 minutes ago Exited (0) 8 minutes ago             sleepy_kowalevski

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 (including stopped ones):

You can use docker ps command with various options to optimize output and gain more details about Docker containers.  The -a flag with docker ps command is used to show 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

If you need information about the containers you have recently created, use the -l option with the docker ps command. This option functions similarly to the -a option but displays more details and lists recently created containers in addition to running or stopped containers:

docker ps -l

This command has not been supported in recent versions of Docker. Alternatively, you can use the docker ps -n N command to list the N most recently created containers. For example, to list the last 4 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 (Quiet Mode)

Sometimes you do not need detailed information about containers, fortunately, by using the -q (--quiet) flag with the docker ps command, you can list only 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

The output of this command gives you information about actual and virtual disk utilization by each container.

Actual size indicates disk utilization by the writable container layer, which includes the container’s filesystem and any customization.

The virtual size reports the combined size of all the container’s layers, including the writable layer, any shares, 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 requires having information about containers.

The --filter option offers flexibility to applay advanced filters for listing containers. It allows you to combine some filter options; for example, to list currently stopped containers that publish port 80, run the following command:

docker ps --filter "status=exited" --filter "publish=80"

8. Formatting the Output

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

The --format option allows you to customize the output using Go templating language syntax to obtain more useful information.

By using the --format option, you can list important features and desired information about the containers instead of the full output, enabling you to manage containers more effectively. Here is 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

There are two alternatives for the docker ps command, which report similar output to the docker ps command. Here are other methods 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.

What’s the point of listing Docker containers?

  • Checking container status is helpful to troubleshoot and resolve issues promptly by identifying stopped or failed containers.
  • Ensuring all active containers and removing stopped containers is important for efficient resource management.
  • By listing your containers, you can retrieve container details for other tasks.
  • You can view ports that are currently in use by listing Docker containers.
  • listing containers allows you to monitor container health check results to detect containers with issues.
  • Listing containers, lets you write your own script.

How do I 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

How to 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.

How do I 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“.

How do I 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 ps -q)

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.

Conclusion

Learning how to list Docker containers effectively is essential for anyone working with containerized applications. It provides visibility into the state and details of containers, enabling better management, troubleshooting, and automation.

By using the ‘docker container ls’ command along with its various options and filters, you can tailor container listings to fit your specific operational needs. Always remember to monitor container status regularly and remove unused containers to maintain a clean and efficient environment.

By mastering how to list Docker containers, you gain greater control over your container ecosystem, contributing to smoother deployment and maintenance of modern applications. This knowledge forms the foundation for advanced container management tasks and improved operational workflows.

FAQ

You can list all Docker containers, including stopped ones, by using the command "docker ps -a". This displays all containers regardless of their state, helping to identify inactive or exited containers.

Options such as "--no-trunc" disable column value truncation, "-s" or "--size" shows container file sizes, and "--format" allows custom output formatting using Go templates. These options help tailor the container list to specific needs.

Use the command "docker ps -q" or "docker ps --quiet" to list only the container IDs. This is useful for scripting and automation when you need to reference containers by their IDs.

Yes, Docker containers can be filtered by attributes such as name, ID, status, image, label, and more with the "--filter" option. For example, "docker ps --filter "status=running"" lists only running containers.

You can list containers managed by Docker Compose using the command "docker compose ps" from within the Compose project directory. This shows containers defined in the Compose configuration.

Alternatives include "docker container ls" and "docker container list". These commands produce similar outputs to "docker ps" but may be preferred in scripts or documentation for clarity.

Listing Docker containers helps monitor container status, troubleshoot problems, manage resources efficiently, track port usage, and maintain container health. It ensures an organized and functional Docker environment.

Use "docker ps -n" followed by the number of containers to list the most recently created ones, for example, "docker ps -n 4" lists the last four created containers. Note that the 'docker ps -l' command is deprecated.

Yes, you can list all Docker container IP addresses using the command "docker inspect" with a filter, such as "docker inspect -f '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)", which inspects all container IDs.

Leave a Reply

Your email address will not be published. Required fields are marked.