Listing containers in Docker
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values.
List all running containers in Docker
We will start by listing the running containers. To do this, we can use docker ps
the command.
$ docker ps
This command lists all of the currently running containers, which can be seen under the STATUS column in the example output below. As shown here, we can also determine the approximate amount of time a container has been up and running.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c955bac1a84 ubuntu "bash" About a minute ago Up About a minute musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp distracted_napier
We can also use docker container ls
the command which returns the same output.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c955bac1a84 ubuntu "bash" 14 minutes ago Up 14 minutes musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 15 minutes ago Up 14 minutes 80/tcp distracted_napier
In addition to STATUS, the following details are returned.
- CONTAINER ID, which is a unique identifier for the container;
- CONTAINER IMAGE, which is the image we use to build the container;
- COMMAND responsible for running the container;
- Port mapping between host and container.
List running and exited containers in Docker
In addition to listing running containers, we can also list running and exited containers. We need to append -a
the tag to the command we used earlier.
By default, these commands only show running containers; however, using this tag you get commands to list exited containers as well.
Order:
$ docker ps -a
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 4 minutes ago Exited (0) 2 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" 52 minutes ago Exited (0) About a minute ago musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 53 minutes ago Up 58 seconds 80/tcp distracted_napier
In the output under the Status column above, you can notice that the first two containers are not running, while the last container is running.
List a specific number of containers in Docker
We can also display a specific number of containers in our system. Let’s say we want to display only the first two containers. We can -n
do this by using the -n tag alongside the command we used above.
This will display both running and exited but not more than the specified number.
Order:
$ docker container ls -n 2
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 31 minutes ago Exited (0) 29 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" About an hour ago Exited (0) 27 minutes ago musing_morse
Likewise, using docker ps
the command will also list the exact number of containers we specify below. Here is how we can achieve it.
Order:
$ docker ps -a -n 2
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 33 minutes ago Exited (0) 31 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" About an hour ago Exited (0) 29 minutes ago musing_morse
List only exited containers in Docker
Using filters, we can also list only containers that are not currently running. Use -f
the <container-name> tag to specify the condition we want to meet, in this case, we only want containers with an exit status returned.
We can also add multiple filters if we want containers returned based on other things. This is how we can list containers with exit status.
Order:
$ docker container ls --filter "status=exited"
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 51 minutes ago Exited (0) 49 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" 2 hours ago Exited (0) 48 minutes ago musing_morse
List recently created containers in Docker
It is also possible to return the latest container using -latest
the tag. This will return the current container that we most recently created.
Order:
$ docker container ls --latest
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 55 minutes ago Exited (0) 53 minutes ago epic_jackson
List containers by ID in Docker
We can also list containers based on their IDs, and we can -q
do this using , also known as the quiet option, as shown below.
Order:
$ docker container ls -q
Output:
d780996c499a
f7509cd49142
72c8debe5efa
List containers and their sizes in Docker
We can also list the containers next to their size. We can identify those that are taking up the maximum memory size, among other details.
We can -s
do this by using the tag, also known as the size tag.
Order:
$ docker container ls -s
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
d780996c499a 4ac4842c584e "/opt/sonarqube/bin/…" 3 minutes ago Up 3 minutes 9000/tcp amazing_benz 73.2kB (virtual 520MB)
f7509cd49142 5285cb69ea55 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 27017/tcp boring_wilson 0B (virtual 698MB)
72c8debe5efa rabbitmq "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp determined_curran 0B (virtual 221MB)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Article URL:https://www.jiyik.com/en/xwzj/opersys_10091.html
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
Publish Date:2025/03/26 Views:97 Category:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
Publish Date:2025/03/26 Views:125 Category:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
Publish Date:2025/03/26 Views:202 Category:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
Publish Date:2025/03/26 Views:88 Category:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:140 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application