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
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