hosting image
List-Docker-Containers

List Docker Containers: A Comprehensive Guide

Listing containers is a crucial step when working with containerized applications. It helps you see which containers are running on a Docker host, monitor their status, use resources better, and spot potential issues. This process also aids in load balancing, organizing where containers are placed, and ensuring security by checking for any unauthorized or harmful containers.

Viewing Running Containers

Use the `docker ps` command to list only the currently running containers. If you’ve created multiple containers, some may be running while others are stopped. By running the `docker ps` command, you’ll see a list of only the active containers by default.

List-Docker-containers

Understanding the Columns in the Output:

After running the `docker ps` command, you’ll see a list of columns for each active container, showing various details:

Container ID: This column displays the unique ID assigned to each running container.

Image: The image used to create each container is shown here.

Command: This column shows the command that runs in the background to start and manage the container.

Created: The timestamp when each container was created.

– Status: This indicates the current state of each container, such as running, exiting, pausing, removing, or restarting.

– Ports: This column shows the ports mapped to each container, allowing you to access services inside the container using the host machine’s ports. For example, if port 8080 on the host is mapped to port 80 in the container, you can access the container’s web service via `http://localhost:8080`.

– Names: Each container has a unique name assigned to it.

List-Docker-Containers

Viewing All Containers

To list all containers, including the stopped ones, use the `docker ps -a` command. This will display every container, whether it’s running, paused, restarting, or stopped.

List-Docker-Containers

Distinguishing Running and Stopped Containers in the Output:

When you run the `docker ps -a` command, you can tell the difference between running and stopped containers by checking the ‘STATUS’ column. If the status shows something like “up 3 hours,” it means the container has been running for the past 3 hours. On the other hand, if it says “Exited 4 hours ago,” the container was stopped 4 hours ago.

See also  How to Upgrade Debian 10 to Debian 11?

List-docker-Containers

Listing the Most Recently Created Containers

The `docker ps -l` command was used to list the most recently created container. However, the `-l` option has been deprecated in newer versions of Docker. Instead, you can use `docker ps -n 1` to view the latest container. With the `docker ps -n 1` command, you need to specify the number of containers you want to list after the `-n` keyword. For example, to see the last 5 containers created, you would run `docker ps -n 5`.

List-Docker-Containers

Understanding the Usefulness of Listing the Latest Container in Different Scenarios

●      Rollback When Issues Arise After an Update:

If you frequently deploy updates to your containerized application, and something goes wrong after the most recent update, this command can be very helpful. It allows you to quickly identify the latest containers created since the update, making it easier to roll back to a previous, stable version.

●      Resource Limitations for Container Deployment:

If your system has limited resources such as CPU, RAM, or storage, and you want to allocate them more efficiently, it’s important to track how much of these resources the most recent containers are using. This command helps you list the latest containers, so you can adjust their configurations to optimize resource usage.

Disabling Truncation

The `docker ps –no-trunc` command is used to prevent the truncation of container names. When you run the `docker ps` command, you may notice that some values in the output, such as container IDs or image names, are shortened, which can make them difficult to read. To avoid this, you can use the `docker ps –no-trunc` command. This will disable Docker’s default truncation, allowing you to see the full values in the output.

Viewing Complete Values in the Output:

Here is an example of the output you will see after disabling Docker’s truncation with the `docker ps –no-trunc` command:

List-Docker-Containers

Listing Only Container IDs (Quiet Mode)

The `docker ps -q` command is used to list only the container IDs in quiet mode. Often, the extra details such as images, ports, and names in the `docker ps` output may not be necessary for your needs. If you’re only interested in seeing the container IDs without the additional information, you can use the `docker ps -q` command. This will adjust the output to show only the container IDs.

See also  What’s the Difference Between BIOS and UEFI

List-Docker-containers

Ideal Scenarios for Using Only Container IDs:

Orchestration Environments

Many organizations, especially larger enterprises, rely on orchestration tools like Kubernetes and Docker Swarm. These tools often use container IDs to manage and schedule containers efficiently. The `docker ps -q` command allows you to retrieve container IDs, enabling you to perform operations on containers within an orchestration environment.

Connecting Containers

In a distributed system, it’s common to establish connections between containers for resource sharing, network communication, and scalability. To set up these connections, you need the specific container IDs. The `docker ps -q` command helps you quickly gather the required IDs to make these connections.

Checking Container Size

The `docker ps –size` command shows the size of each container. As mentioned earlier in the ‘Listing Latest Containers’ section, limited resources like CPU, RAM, and storage need careful monitoring. This command allows you to check the disk space used by each container, helping you quickly identify containers that are consuming too much space, so you can take action to manage resources more effectively.

List-Docker-Containers

Understanding the Extra Size Column in the Output

When you run the `docker ps –size` command, an extra column will appear in the output that shows both the Actual Size and the Virtual Size used by each container.

– Actual Size: This refers to the size used by the writable container layer, which includes the container’s filesystem and any changes made to it.

– Virtual Size: This represents the total disk space needed if each layer in the container was stored separately and not shared. It includes the combined size of all the layers in the container, such as base images, shared layers, and additional layers.

Customizing the Output

The `docker ps –format` command allows you to customize the output format by selecting only the columns you want to display when listing your containers. To do this, you can use the `–format` option with a Go template string, which is built into Docker and based on the Go programming language. This syntax is easy to use directly with Docker commands. The command would look like this: `docker ps --format "GOTEMPLATE"`.

Go Template String Syntax:

`docker ps --format "ID: {{.ID}}, Name: {{.Names}}, Image: {{.Image}}, Status: {{.Status}}"`

In this syntax, the double curly brackets `{{}}` represent placeholders that correspond to specific container properties, such as ID, Names, Image, and Status. When you run the command, these placeholders will be replaced with the actual values.

See also  What is Firewall?

Examples of Customizing the Displayed Information

Here’s how you can customize the output to show only the Container ID, Image, and Name:

List-Docker-Containers

Now, let’s customize the output to display the latest containers, including the Container ID, Status, and an additional column for SIZE:

List-Docker-Containers

Using Advanced Filters

The `docker ps –filter` command lets you apply advanced filters to list containers based on specific attributes or conditions. To use it, simply add the `–filter` option followed by a key-value pair. The “key” refers to the container attribute you want to filter by, and the “value” is the condition you want to apply. One useful feature of the filter option is that you can use it multiple times with different conditions.

Filtering by Different Criteria such as Name, Label, Status, etc.

For example, to filter by the container name, you can use the following command to display only the container with the name “admiring_benz”:

`docker ps --filter "name=admiring_benz"`

List-Docker-Containers

Filtering by Status: You can filter the output based on various container statuses, such as “exited,” “created,” or “running.”

For example, you can use the following commands to filter containers by their status:

- `docker ps --filter "status=running"`

- `docker ps --filter "status=exited"`

- `docker ps --filter "status=created"`

List-Docker-Containers

Filtering by Label: You can apply multiple filters to the output by using the `–filter` keyword for both the status attribute and label conditions. For example, to filter containers based on the “created” status and a specific label, you can use the following command:

`docker ps --filter "status=created" --filter "label=com.example.version=1.0"`

List-Docker-Containers

Conclusion

The `docker ps` command is used to list all running containers. By combining `docker ps` with different options like `docker ps -n 5`, `docker ps -l`, `docker ps -a`, and `docker ps -q`, you can customize the output to display the latest containers, show only container IDs, or list containers in various states (such as running, stopped, or created).

You can also modify the output format using the `–format` option, followed by a Go Template, to display the container information in a specific way (e.g., `docker ps –format “TEMPLATE”`). Advanced filters can be applied with the `–filter` option to show containers based on specific attributes.

Additionally, the actual and virtual sizes of containers can be viewed in an extra column by using the `–size` keyword with the `docker ps` command (e.g., `docker ps –size`).

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Setup Your Server