How to display all running and stopped containers?

Displaying All Running and Stopped Containers

As a Docker expert and mentor, I'm happy to help you with your question on displaying all running and stopped containers. In this response, I'll guide you through the necessary steps and provide examples to ensure you have a comprehensive understanding of the topic.

Listing Running Containers

To display all the running containers on your system, you can use the docker ps command. This command will show you the containers that are currently running.

docker ps

The output of this command will include the following information for each running container:

  • Container ID
  • Image
  • Command
  • Created
  • Status
  • Ports
  • Names

If you want to see more detailed information about the running containers, you can use the docker ps -a command, which will display all containers, including those that are stopped.

docker ps -a

This command will provide additional details, such as the exit code and the time when the container was started or stopped.

Listing Stopped Containers

To display all the stopped containers on your system, you can use the docker ps -a command, as mentioned earlier. This command will show you both the running and stopped containers.

docker ps -a

The output of this command will include the same information as the docker ps command, but it will also show the stopped containers.

Filtering Container Listings

If you want to filter the container listings based on specific criteria, you can use the --filter or -f option with the docker ps command. For example, to list only the containers that are currently running, you can use the following command:

docker ps --filter "status=running"

Similarly, to list only the containers that are currently stopped, you can use the following command:

docker ps --filter "status=exited"

You can also combine multiple filters to narrow down the results. For instance, to list all containers that have a specific name, you can use the following command:

docker ps -a --filter "name=my-container"

Visualizing Container Lifecycle

To better understand the lifecycle of Docker containers, let's use a Mermaid diagram:

graph TD A[Create Container] --> B[Start Container] B --> C[Running Container] C --> D[Stop Container] D --> E[Exited Container] E --> F[Remove Container]

This diagram illustrates the different states a container can go through during its lifecycle, from creation to removal. The docker ps and docker ps -a commands allow you to view the containers in their current state, whether they are running or stopped.

By understanding how to list both running and stopped containers, you can effectively manage your Docker environment and monitor the status of your applications.

0 Comments

no data
Be the first to share your comment!