How to list running and stopped Docker containers?

DockerDockerBeginner
Practice Now

Introduction

In this tutorial, we will explore the essential techniques for listing running and stopped Docker containers. Understanding how to effectively manage and monitor your Docker environment is crucial for efficient container-based application development and deployment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/restart("`Restart Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/ps -.-> lab-411564{{"`How to list running and stopped Docker containers?`"}} docker/restart -.-> lab-411564{{"`How to list running and stopped Docker containers?`"}} docker/start -.-> lab-411564{{"`How to list running and stopped Docker containers?`"}} docker/stop -.-> lab-411564{{"`How to list running and stopped Docker containers?`"}} docker/ls -.-> lab-411564{{"`How to list running and stopped Docker containers?`"}} end

Understanding Docker Containers

Docker is a popular containerization platform that allows developers to package and deploy applications in a consistent and reproducible way. Containers are lightweight, standalone, and self-contained units that encapsulate an application, its dependencies, and its runtime environment. This makes it easier to develop, test, and deploy applications across different environments, from a developer's laptop to production servers.

What are Docker Containers?

Docker containers are a way to package and distribute applications. They provide a standardized and isolated environment for running applications, ensuring that the application and its dependencies are bundled together and can be easily deployed on any system that has Docker installed. Containers are created from Docker images, which are like blueprints or templates for the container.

Benefits of Docker Containers

  • Consistency: Containers ensure that the application and its dependencies are packaged together, eliminating the "it works on my machine" problem.
  • Portability: Containers can be easily moved between different environments, from development to production, without the need for complex configuration changes.
  • Scalability: Containers can be easily scaled up or down, allowing you to adjust the resources allocated to your application based on demand.
  • Efficiency: Containers are lightweight and share the host's operating system, making them more efficient than traditional virtual machines.

Docker Architecture

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon (the server) to perform various operations, such as building, running, and managing containers. The Docker daemon runs on the host machine and is responsible for managing the containers and their lifecycle.

graph LD subgraph Docker Architecture client[Docker Client] -- API --> daemon[Docker Daemon] daemon -- Pulls Images --> registry[Docker Registry] daemon -- Manages --> container[Docker Containers] end

Docker Images and Containers

Docker images are the blueprints for creating containers. They contain the application code, dependencies, and the necessary configuration to run the application. Containers are the running instances of Docker images, and they provide an isolated and consistent environment for running the application.

Listing Running Docker Containers

Once you have Docker containers running, you may need to list and manage them. To list the running Docker containers, you can use the docker ps command.

Listing Running Containers

To list all the running Docker containers, use the following command:

docker ps

This will display a table with information about the running containers, including the container ID, the image used to create the container, the command being executed, the time the container was created, the status, and the ports.

You can also add additional options to the docker ps command to customize the output. For example:

docker ps -a

This will list all containers, including those that are not currently running.

docker ps --format "{{.ID}}\t{{.Image}}\t{{.Status}}"

This will display the container ID, image, and status in a tabular format.

Filtering Running Containers

You can also filter the list of running containers using various options. For example:

docker ps -f "status=running"

This will only list the containers that are currently running.

docker ps -f "name=mycontainer"

This will only list the containers with the name "mycontainer".

docker ps --format "{{.ID}}\t{{.Image}}\t{{.Status}}" -f "status=running"

This will list the container ID, image, and status for all running containers in a tabular format.

By understanding how to list running Docker containers, you can easily manage and monitor the containers in your Docker environment.

Listing Stopped Docker Containers

In addition to listing running containers, you may also need to list stopped or exited containers. This can be useful for troubleshooting, cleaning up unused containers, or restarting stopped containers.

Listing Stopped Containers

To list all the stopped Docker containers, use the following command:

docker ps -a -f "status=exited"

This will display a table with information about the stopped containers, including the container ID, the image used to create the container, the command that was executed, the time the container was created, the status, and the ports.

You can also use the --format option to customize the output:

docker ps -a -f "status=exited" --format "{{.ID}}\t{{.Image}}\t{{.Status}}"

This will display the container ID, image, and status in a tabular format.

Filtering Stopped Containers

You can also filter the list of stopped containers using various options. For example:

docker ps -a -f "status=exited" -f "name=mycontainer"

This will only list the stopped containers with the name "mycontainer".

docker ps -a -f "status=exited" --format "{{.ID}}\t{{.Image}}\t{{.Status}}" -f "name=mycontainer"

This will list the container ID, image, and status for all stopped containers with the name "mycontainer" in a tabular format.

By understanding how to list stopped Docker containers, you can easily manage and monitor the containers in your Docker environment, even those that are not currently running.

Summary

By the end of this guide, you will have a solid understanding of how to list both running and stopped Docker containers, empowering you to better manage and troubleshoot your Docker-based applications. Mastering these skills will help you streamline your Docker workflow and maintain a clear overview of your container ecosystem.

Other Docker Tutorials you may like