Skip to main content

A Brief Guide about Docker for Developer in 2023

  What is Docker? Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is based on the idea of containers, which are a way of packaging software in a format that can be easily run on any platform. Docker provides a way to manage and deploy containerized applications, making it easier for developers to create, deploy, and run applications in a consistent and predictable way. Docker also provides tools for managing and deploying applications in a multi-container environment, allowing developers to easily scale and manage the application as it grows. What is a container? A container is a lightweight, stand-alone, and executable package that includes everything needed to run the software, including the application code, system tools, libraries, and runtime. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. It al...

A Comprehensive Guide to Listing Docker Containers: Mastering Container Management

 

Listing containers is an essential task if you are working with containerized applications as this allows you to see the running container on a Docker host, monitor their health, efficiently utilize the resource, help identify potential causes of the problem, help balance loads, organize container placement, and also take care of security by checking the presence of unauthorized or malicious containers.

You might have created many containers. Some are running, but some are stopped. Now, you will execute the ‘docker ps’ command for listing docker containers, but executing this command will show you only running containers by default.

react tooltip

Once you have executed the ‘docker ps’ command, you can see the following output against each running container based on seven different columns.

  1. Container Id: This column contains the identity mapped to each container running.

  2. Image: This is an image of each container specifically used to create that container.

  3. Command: Command executed in the background to initialize and run each container.

  4. Created: Timestamp when each container is created.

  5. Status: The current state of each container that can be running, exited, paused, removing, restarting etc.

  6. Ports: Ports are bind to each container, and they will help you to access the service inside each container by utilizing the host port. For Example, if host port 8080 is bind to container port 80, then you can be able to access the container web content by using ‘http://localhost:8080’ URL on your local machine.

  7. Names: Each running container has a unique name assigned to them.

react tooltip

If you want to see all containers, add a keyword with the ‘docker ps’ command, i.e., ‘-a’. This command will show you all containers, whether they are running, restarting, paused, or stopped.

react tooltip

In the output of the ‘docker ps -a’ command, you can distinguish between running and stopped containers by looking at the ‘STATUS’ column. If the value is ‘up 3 hours’, the container has been running for the last 3 hours, and if the value is ‘Exited 4 hours go’, the container was stopped 4 hours ago.

react tooltip

docker ps -l’ is used to list the latest container created, but the ‘-l’ keyword is depreciated in recent versions of docker. Instead of ‘-l’, you can use ‘docker ps -n 1’ to see the latest one. Using ‘docker ps -n 1’, it is important to set the limit of showing the latest containers; you must add the number of limits after the ‘-n’ keyword. For Example, if you want to list the last 5 latest containers created, then you can execute ‘docker ps -n 5’.

react tooltip

Suppose you regularly deploy new updates to your containerized application, and anything goes wrong after the last update. In that case, this command will help you easily identify most latest containers created since the last update for immediate rollback to the older version.

Suppose you have limited resources(CPU/RAM/Storage etc.) and want an effective allocation of resources; for that, you need to have a track of the resource utilized by the most recent containers created, and this command will help you list them all to make a change in their configuration to ensure efficiency.

When you run the command ‘docker ps’ you will find out that in output, the values of a few columns are truncating, making it difficult to understand, especially when you need full container ids or image names. For that purpose, you need to use ‘docker ps –no-trunc’, which will turn off the docker default truncating feature and give you a full view of values in the output.

Following is the example output after disabling docker default truncation by using the ‘docker ps –no-trunc’ command:

react tooltip

Most of the time, in the ‘docker ps’ command output, the additional information except Container ids is not useful for you, and instead of showing images, ports, names, etc., against each container in the output, you only want to list the container ids, then for this purpose, there is a specific command, i.e., ‘docker ps -q’ that will customize your output to only display container ids.

react tooltip


refine.new enables you to create React-based, headless UI enterprise applications within your browser that you can preview, tweak and download instantly.

🚀 By visually combining options for your preferred ✨ React platform, ✨ UI framework, ✨ backend connector, and ✨ auth provider; you can create tailor-made architectures for your project in seconds. It feels like having access to thousands of project templates at your fingertips, allowing you to choose the one that best suits your needs!

refine blog logo


Most companies, especially enterprises, use orchestration tools like Kubernetes and docker swarm. These orchestration tools often utilize Container IDs to manage and schedule containers. You can retrieve a container ID and run operations on containers in the orchestration environment using docker ps -q.

In a distributed architecture, we usually must establish a connection between containers for resource sharing, network connectivity, and scalability. For creating a connection between containers, we need to have IDs specific to them. Using the command ‘docker ps -q’, we can quickly fetch the required ids to create a connection.

As we have discussed earlier in the ‘Listing Latest Containers’ scenarios section that, we might have limited resources, so to avoid unnecessary usage, we need to monitor the resource utilization by each container. This specific command will facilitate us by representing the disk space utilized by each container and allow us to quickly identify containers that consume large amounts of space on the disk to take the appropriate actions.

react tooltip

The ‘docker ps --size’ command will add an additional column to the list of containers output that represents the Actual Size(overall size) and Virtual Size utilized by each container.

Actual Size: The size utilized by the writable container layer consists of the container’s filesystem and any modifications made.

Virtual Size: If each layer in a container were to be separated and not shared, the virtual size is the total amount of disk space required. The combined size of all the container's layers, which include any shares, base pictures, and additional layers, comes under the virtual size.

Suppose you want to get the customized output that will only contain the column of your choice while listing your containers. In that case, you can use ‘docker ps’ with the keyword ‘--format’ along with the Go template string that is directly integrated with docker and can be used simply with the docker command because its syntax is native to the ‘Go Programming Language’ on which the docker is itself built. The command will look something like this ‘docker ps –format “GOTEMPLATE” ’.

Go Template String Syntax: docker ps --format "ID: {{.ID}}, Name: {{.Names}}, Image: {{.Image}}, Status: {{.Status}}"

Double curly brackets {{}} are used to denote placeholders that relate to particular container properties such as ID, Names, Image, and Status. These placeholders will be replaced by actual values when you run the command.

Let’s customize an output that only displays Container ID, Image, and Name:

react tooltip

Let’s customize an output that displays the latest containers with Container ID, Status, and additional column SIZE:

react tooltip

Docker also provides advanced filtering options that will allow you to list the containers based on different attributes and conditions of your choice. All you need to do is to add ‘--filter’ keyword with the key-value pair. ‘Key’ will be the name of an attribute of the container on which you want to filter, and ‘Value’ will be the condition you want to apply to that attribute. An interesting fact about the filter keyword is that you can repeat it multiple times with different conditions.

Filtering Based on Name: Only show the container with the name ‘admiring_benz’(Command: ‘docker ps –filter “name=admiring_benz”’)

react tooltip

Filtering Based on Status: Filter output based on different statuses e.g exited, created or running etc.

Commands: docker ps –filter “status=running”, docker ps –filter “status=exited”, docker ps –filter “status=created”

react tooltip

Filtering Based on Label: Filter output by using Filter keyword multiple times for status attribute and label conditioning.(Command: docker ps --filter "status=created" --filter "label=com.example.version=1.0")

react tooltip

  1. 'docker ps' command can be use to list all containers that are running.

  2. By using 'docker ps' command with other options('docker ps -n 5'/'docker ps -l', 'docker ps -a', 'docker ps -q') we can achieve the output with the list of the latest containers, containers with ids only and containers in different states(running, stopped, created, etc.)

  3. The output of 'docker ps' command can be customized by adding a format keyword followed by the Go Template in the command(e.g docker ps –format "TEMPLATE").

  4. We can also use advance filters based on attributes to get the result with specific containers against 'docker ps' command.

  5. The actual and Virtual size of each container can be added and viewed in additional column of the container list by using '--size' keyword with 'docker ps' command(e.g docker ps --size).

  1. Container listing is useful for container management to view all created containers comprehensively.

  2. It is useful in connecting different containers to share resources and monitor container-specific health and performance.

  3. Integrating container listing commands in scripts, third-party monitoring systems, and orchestration tools will help to accomplish automated workflows.

  4. Container listing will also assist in identifying the bug in the containerized environment by using them with logging, allowing you to examine each container's details.

Comments

Popular posts from this blog

JavaScript: What the heck is a Callback?

  What is a Callback? Simply put:   A callback is a function that is to be executed  after  another function has finished executing — hence the name ‘call back’. More complexly put:   In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions. Functions that do this are called  higher-order functions . Any function that is passed as an argument is called a  callback function . ^ That’s a lot of words. Lets look at some examples to break this down a little more. Why do we need Callbacks? For one very important reason — JavaScript is an event driven language. This means that instead of waiting for a response before moving on, JavaScript will keep executing while listening for other events. Lets look at a basic example: function first(){ console.log(1); } function second(){ console.log(2); } first(); second(); As you would expect, the function  first  is executed f...

Flutter — Clean Code

  Introduction: Clean code is essential in every programming language to make the code more readable, maintainable, and understandable. The same is true for Flutter. Clean code practices in Flutter are the best way to ensure that your code is easy to understand, easy to maintain, and easy to modify. In this article, we will discuss some of the best clean code practices in Flutter with examples. Follow Flutter Naming Conventions: When writing code in Flutter, it is essential to follow the naming conventions recommended by the Flutter team. Flutter follows the Dart language naming conventions. These conventions help other developers to understand your code easily. Here is an example of how to name a class in Flutter: // Good naming convention class MyClass {} // Bad naming convention class my_class {} Use Descriptive Variable and Function Names: Use descriptive variable and function names so that other developers can understand the purpose of the variable or function. Avoid using...

A Brief Guide about Docker for Developer in 2023

  What is Docker? Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is based on the idea of containers, which are a way of packaging software in a format that can be easily run on any platform. Docker provides a way to manage and deploy containerized applications, making it easier for developers to create, deploy, and run applications in a consistent and predictable way. Docker also provides tools for managing and deploying applications in a multi-container environment, allowing developers to easily scale and manage the application as it grows. What is a container? A container is a lightweight, stand-alone, and executable package that includes everything needed to run the software, including the application code, system tools, libraries, and runtime. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. It al...