List only stopped containers in Docker
Docker provides us with some useful commands that we can use to manage our images and containers, but some commands are not obvious. For example, we can use the Docker command docker ps
to display a list of running containers, or use the docker command docker ps -a
to display a list of all containers, whether stopped or running.
One feature that is not obvious is to display a list of only stopped containers. In this tutorial, we will learn how to display a list of only stopped containers in Docker.
Pull the Nginx image from Docker Hub
In this tutorial, we will use the Nginx image to run containers from it and use these containers to achieve the goals of this article.
Open a new terminal window on your computer using the keyboard shortcut ALT+F12 and then use the following command to pull the Nginx image from the docker hub.
~$ docker pull nginx
Since we did not specify a tag, this command will pull the latest version of Nginx. We can view the download details in the terminal window, as shown below.
Using default tag: latest
latest: Pulling from library/nginx
e9995326b091: Pull complete
71689475aec2: Pull complete
f88a23025338: Pull complete
0df440342e26: Pull complete
eef26ceb3309: Pull complete
8e3ed6a9e43a: Pull complete
Digest: sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
List Docker images
Once the download is complete, we can verify that the Nginx image has been downloaded using the following command which lists all images.
~$ docker image ls
If you have installed other images before, using this command will list them all. In our case, we have just displayed the image used in this tutorial to verify that the image has been downloaded.
The image details are as follows:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 76c69feac34e 2 days ago 142MB
Running a container from the Nginx image
For testing purposes, we will only run two containers named container-one and container-two . In the same terminal window, execute the following commands one after the other to run two containers using the nginx image.
Run container-one :
~$ docker run --name container-one -d -p 3000:80 nginx
Output:
9e70a29947c373df2abd4748271d252f297c6a06669f7cffb04fe95ee5fca671
Run container-two :
~$ docker run --name container-two -d -p 5000:80 nginx
Output:
43cd017b94062e47c6c3f868ed74fb5ca7920522e870eceb58115e388e9d43a5
请注意
, the first container exposes port 3000 on the host to listen on port 80 on the container, while the second container exposes port 5000 on the host to listen on the same port as the previous container.
List running containers
In the previous section, we ran two containers named container-one and container-two . To verify that these containers are running, we can use the following command, which lists all running containers.
~$ docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43cd017b9406 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:5000->80/tcp container-two
9e70a29947c3 nginx "/docker-entrypoint.…" 24 minutes ago Up 24 minutes 0.0.0.0:3000->80/tcp container-one
Stop a running container
Before listing the stopped containers, we have to stop a container first, we can use the following command to stop the container using its name. You can run and stop multiple containers freely.
~$ docker stop container-one
Output:
container-one
When we execute docker ps
the command, we will find that only container-two is running. In the next section, we will learn how to list only stopped containers, which is the main goal of this tutorial.
Use the docker ps command to list stopped containers
docker ps
The command has a --filter flag that we can specify to filter the containers based on a condition. The value of the --filter flag expects the condition that will be used to provide the output.
In the same terminal window, use the following command to filter for containers with a status of exited.
~$ docker ps --filter "status=exited"
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e70a29947c3 nginx "/docker-entrypoint.…" 48 minutes ago Exited (0) 18 minutes ago container-one
Use the docker container ls command to list stopped containers
This command has the same syntax as the previous example. docker container ls
The command has a --filter flag that we can specify to filter containers based on a condition. Since the --filter flag is the same, we only need to provide the condition to filter containers with a status of exited, as follows.
~$ docker container ls --filter "status=exited"
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e70a29947c3 nginx "/docker-entrypoint.…" 48 minutes ago Exited (0) 18 minutes ago container-one
请注意
The --filter flag should be provided as a key-value pair, if you have multiple filters make sure to provide multiple flags.
In this article, we used a filter called status to filter containers based on their status. However, the docker documentation provides other strategies that we can use to filter containers.
Summarize
In this tutorial, we learned how to leverage the Nginx container to list only stopped containers in Docker. The two methods covered include Listing Stopped Containers Using the ps Command and Listing Stopped Containers Using the containers ls command.
Both commands have a --filter flag that accepts key-value pairs to filter containers based on specific criteria.
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.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category: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 list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Docker build command with multiple parameters
Publish Date:2025/03/25 Views:138 Category:Docker
-
docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha
The difference between CMD and ENTRYPOINT in Docker
Publish Date:2025/03/25 Views:141 Category:Docker
-
Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container
Use the Dockerfile to create a directory in the container using the Mkdir command
Publish Date:2025/03/25 Views:73 Category:Docker
-
Docker containers have become the de facto way to manage software and dependencies in different environments. When working with real-world applications, you will undoubtedly need to create a Dockerfile before you can build your application