Software Development

Listing Docker Containers

Hello. In this article let us delve into understanding the Docker commands to list and filter the containers.

1. What is Docker?

Docker is an open-source platform used for the containerization of applications. It allows developers to package their applications along with their dependencies, libraries, and other necessary components into a single container that can run reliably and consistently on any platform. The containerization technology provided by Docker ensures that the application behaves the same way regardless of the underlying infrastructure. Some benefits of Docker are:

  • Portability: Docker containers can run on any platform, regardless of the underlying infrastructure. This makes it easy to move applications between development, testing, and production environments.
  • Scalability: Docker allows you to quickly and easily scale your application by adding or removing containers as needed, without having to make changes to the underlying infrastructure.
  • Isolation: Docker provides a high level of isolation between applications, ensuring that each container runs independently of others, without interfering with each other.
  • Efficiency: Docker containers are lightweight and efficient, consuming fewer resources than traditional virtual machines. This allows you to run more applications on the same hardware.
  • Consistency: Docker ensures that applications behave the same way across different environments, making it easier to test and deploy new versions of your application.
  • Security: Docker provides built-in security features that help protect your applications from external threats. Docker containers are isolated from each other and the underlying infrastructure, reducing the risk of attacks.

Overall, Docker provides a powerful platform for building, testing, and deploying applications that are both efficient and reliable.

1.1 What is Docker used for?

It is used for –

  • For environment replication, while the code runs locally on the machine.
  • For numerous deployment phases i.e. Dev/Test/QA.
  • For version control and distributing the application’s OS within a team.

1.2 Basic Docker Terminology?

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java.
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar.
  • Engine: The code that manages, creates, and runs the Docker containers.
  • Hub: A public developer’s registry to distribute their code.
  • Repository: A collection of Docker-related images i.e. different versions of the same application.

1.3 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

2. Exploring Docker Container Listing Commands

Docker containers are lightweight, standalone, and executable packages that contain all the necessary components to run a piece of software, including the code, runtime, libraries, and system tools. When working with Docker, it’s essential to be able to list the containers that are currently running or have been created.

CommandDescription
docker ps -aTo list all Docker containers, regardless of their current state (running or stopped). This command displays a list of all containers along with their details such as container ID, image, status, ports, and names.
docker psTo list only the containers that are currently running. This command provides a list of running containers, showing their container ID, image, command, creation time, status, and ports.
docker ps -a --filter "status=exited"To view only the containers that have stopped. This command filters the output to display only the containers that are in an “exited” state, meaning they were running but have stopped.
docker ps -aqTo obtain only the IDs of containers. This command returns a list of container IDs, which can be useful for scripting or automation purposes.

3. Exploring Docker Container Filtering Commands

Filtering Docker containers allows you to narrow down the list of containers based on specific criteria, such as their status, labels, or names. This can be particularly useful when managing a large number of containers or when looking for containers with particular attributes.

FilterDescriptionExample
Filter by NameTo filter containers by name, you can use the --filter "name=your_container_name" option with the docker ps command. This command will display only the containers whose names match the specified name.docker ps --filter "name=your_container_name"
Filter by StatusTo filter containers by their status (running, exited, or paused).
  • docker ps --filter "status=running"
  • docker ps -a --filter "status=exited"
  • docker ps --filter "status=paused"
Filter by LabelDocker containers can be labeled with custom key-value pairs for easier management. This command will display only the containers that have the specified label key with the corresponding value.docker ps --filter "label=key=value"
Combining FiltersYou can combine multiple filters to further refine your search. This command will show only the running containers that have the specified label.docker ps --filter "status=running" --filter "label=key=value"

4. Advanced Customization of Output in Docker

When working with Docker, you can customize the output of various commands to suit your needs. This allows you to format the output in a way that is most useful for your specific use case. Below are some advanced techniques for customizing output in Docker:

TechniqueDescriptionExample
Format Output with Pretty PrintingYou can use the --format option to format the output of Docker commands using Go templates.docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"
Display Specific FieldsBy specifying the fields you want to display, you can tailor the output to include only relevant information.docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
JSON OutputYou can output Docker command results in JSON format, which can be easily parsed by other tools or scripts.docker inspect --format='{{json .}}' container_id
Filtering OutputFiltering allows you to narrow down the output to only include specific information that meets certain criteria.docker ps --format "{{.ID}} {{.Status}}" --filter "status=running"

5. Conclusion

In conclusion, Docker provides a robust ecosystem for managing containers efficiently. Throughout this exploration, we’ve delved into key functionalities, including listing Docker containers, filtering them based on specific criteria, and advancing customization of output. These capabilities empower users to streamline container management, enhance visibility, and tailor outputs to meet diverse operational needs. By leveraging Docker’s versatile toolkit, organizations can optimize resource allocation, streamline deployment workflows, and bolster overall system agility. As Docker continues to evolve, its versatility and scalability remain pivotal in driving modern containerization strategies and fostering innovation across various industries.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button