How to list running Docker containers?

Listing Running Docker Containers

To list the running Docker containers on your system, you can use the docker ps command. This command will display information about the currently running containers, including their container ID, image, command, creation time, status, and ports.

Here's the basic syntax for the docker ps command:

docker ps [options]

Here are some common options you can use with docker ps:

  • -a: List all containers (running and stopped)
  • -q: Only display the numeric IDs of the containers
  • -n=<number>: List the most recent <number> of containers
  • -f <filter>: Filter output based on conditions provided

For example, to list all running Docker containers, you can use the following command:

docker ps

This will output something like this:

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                  NAMES
a1b2c3d4e5f6   nginx:latest  "nginx -g 'daemon off"   10 minutes ago  Up 10 minutes  0.0.0.0:80->80/tcp     web-server
g7h8i9j0k1l2   redis:latest  "docker-entrypoint.s"   15 minutes ago  Up 15 minutes  0.0.0.0:6379->6379/tcp  redis-cache

This output shows that there are two running containers: a web server running the nginx image and a Redis cache running the redis image.

If you want to list all containers, both running and stopped, you can use the -a option:

docker ps -a

This will show you all the containers on your system, regardless of their current status.

You can also filter the output using the -f option. For example, to list only the containers that are currently running, you can use the following command:

docker ps -f "status=running"

This will only show the containers that are currently in the "running" state.

Overall, the docker ps command is a powerful tool for managing and monitoring your Docker containers. By using the various options, you can customize the output to suit your needs and quickly get an overview of the containers running on your system.

Visualizing Docker Container Lifecycle with Mermaid

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

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

This diagram shows the different states a Docker container can go through during its lifecycle. When you create a new container, it starts in the "Create Container" state. Once you start the container, it moves to the "Running Container" state. From there, you can stop the container, which puts it in the "Exited Container" state. Finally, you can remove the container, which brings it back to the "Create Container" state.

The key takeaways from this diagram are:

  1. Containers can be created, started, stopped, and removed.
  2. The "Running Container" state is the active state where the container is executing its commands.
  3. Stopped containers can be restarted, bringing them back to the "Running Container" state.
  4. Removed containers are deleted from the system, but you can always create a new one.

This lifecycle diagram provides a visual representation of the different stages a Docker container can go through, which can help you better understand the container management process.

0 Comments

no data
Be the first to share your comment!