迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

仅列出 Docker 中已停止的容器

作者:迹忆客 最近更新:2023/04/17 浏览次数:

Docker 为我们提供了一些有用的命令,我们可以使用这些命令来管理我们的镜像和容器,但是有些命令并不明显。 例如,我们可以使用 Docker 命令 docker ps 来显示正在运行的容器列表,或者使用 docker 命令 docker ps -a 来显示所有容器的列表,无论是停止的还是正在运行的。

一个不明显的功能是仅显示已停止容器的列表。 在本教程中,我们将学习如何在 Docker 中显示仅已停止容器的列表。


从 Docker Hub 拉取 Nginx 镜像

在本教程中,我们将使用 Nginx 映像从中运行容器,并使用这些容器来实现本文的目标。

在您的计算机上使用键盘快捷键 ALT+F12 打开一个新的终端窗口,然后使用以下命令从 docker hub 中拉取 Nginx 映像。

~$ docker pull nginx

由于我们没有指定标签,这个命令将拉取最新版本的 Nginx。 我们可以在终端窗口查看下载详情,如下图。

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

列出 Docker 镜像

下载完成后,我们可以使用以下列出所有图像的命令来验证 Nginx 图像是否已下载。

~$ docker image ls

如果之前安装了其他镜像,则使用此命令将全部列出。 在我们的例子中,我们刚刚显示了本教程中使用的图像以验证镜像是否已下载。

镜像细节如下所示:

REPOSITORY                        TAG       IMAGE ID       CREATED       SIZE
nginx                             latest    76c69feac34e   2 days ago    142MB

从 Nginx 镜像运行容器

出于测试目的,我们将只运行两个分别名为 container-onecontainer-two 的容器。 在同一个终端窗口中,依次执行以下命令以使用 nginx 映像运行两个容器。

运行 container-one

~$ docker run --name container-one -d -p 3000:80 nginx

输出:

9e70a29947c373df2abd4748271d252f297c6a06669f7cffb04fe95ee5fca671

运行 container-two:

~$ docker run --name container-two -d -p 5000:80 nginx

输出:

43cd017b94062e47c6c3f868ed74fb5ca7920522e870eceb58115e388e9d43a5

请注意 ,第一个容器在主机上公开端口 3000 以侦听容器上的端口 80,而第二个容器在主机上公开端口 5000 以侦听与前一个容器相同的端口。


列出正在运行的容器

在上一节中,我们运行了两个名为 container-onecontainer-two 的容器。 要验证这些容器是否正在运行,我们可以使用以下命令,它列出了所有正在运行的容器。

~$ docker ps

输出:

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

停止正在运行的容器

在列出已停止的容器之前,我们必须先停止一个容器,我们可以使用以下命令来停止使用其名称的容器。 可以自由运行和停止多个容器。

~$ docker stop container-one

输出:

container-one

当我们执行 docker ps 命令时,会发现只有 container-two 在运行。 在下一节中,我们将学习如何仅列出已停止的容器,这是本教程的主要目标。


使用 docker ps 命令列出已停止的容器

docker ps 命令有一个 --filter 标志,我们可以指定它来根据条件过滤容器。 --filter 标志的值期望将用于提供输出的条件。

在同一个终端窗口中,使用以下命令过滤状态为 exited 的容器。

~$ docker ps --filter "status=exited"

输出:

CONTAINER ID   IMAGE                   COMMAND                  CREATED          STATUS                      PORTS                    NAMES
9e70a29947c3   nginx                   "/docker-entrypoint.…"   48 minutes ago   Exited (0) 18 minutes ago                            container-one

使用 docker container ls 命令列出已停止的容器

此命令与前面的示例具有相同的语法。 docker container ls 命令有一个 --filter 标志,我们可以指定它来根据条件过滤容器。 由于 --filter 标志是一样的,我们只需要提供条件就可以过滤状态为exited的容器,如下。

~$ docker container ls --filter "status=exited"

输出:

CONTAINER ID   IMAGE                   COMMAND                  CREATED          STATUS                      PORTS                    NAMES
9e70a29947c3   nginx                   "/docker-entrypoint.…"   48 minutes ago   Exited (0) 18 minutes ago                            container-one

请注意 --filter 标志应以键值对的形式提供,如果有多个过滤器,请确保提供多个标志。

在本文中,我们使用了名为 status 的过滤器来根据状态过滤容器。 但是,docker 文档提供了我们可以用来过滤容器的其他策略。


总结

在本教程中,我们学习了如何利用 Nginx 容器仅列出 Docker 中已停止的容器。 涵盖的两种方法包括使用 ps 命令列出已停止的容器和使用容器 ls 命令列出已停止的容器。

这两个命令都有一个 --filter 标志,该标志接受键值对以根据特定条件过滤容器。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类: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

发布时间:2025/03/26 浏览次数:131 分类: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

发布时间:2025/03/26 浏览次数:107 分类: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

发布时间:2025/03/26 浏览次数:97 分类: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

发布时间:2025/03/26 浏览次数:124 分类: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

发布时间:2025/03/26 浏览次数:202 分类: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

发布时间:2025/03/26 浏览次数:87 分类: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

发布时间:2025/03/26 浏览次数:140 分类: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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便