列出 Docker 中的容器
本文将探讨用于列出在我们系统中创建的容器的各种命令。这意味着我们应该事先为这些命令创建一些容器来返回值。
列出 Docker 中所有正在运行的容器
我们将从列出正在运行的容器开始。为此,我们可以使用 docker ps
命令。
$ docker ps
此命令列出了当前运行的所有容器,可以在下面示例输出中的 STATUS 列下看到。如此处所示,我们还可以确定容器已启动并运行的大致时间量。
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
我们还可以使用 docker container ls
命令返回相同的输出。
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
除了 STATUS,还返回以下详细信息。
- CONTAINER ID,这是容器的唯一标识符;
- CONTAINER IMAGE,即我们用来构建该容器的镜像;
- 负责运行容器的 COMMAND;
- 主机和容器之间的端口映射。
列出 Docker 中正在运行和退出的容器
除了列出正在运行的容器外,我们还可以列出正在运行和退出的容器。我们需要将 -a
标签附加到我们之前使用的命令中。
默认情况下,这些命令只显示正在运行的容器;但是,使用此标签也可以获得列出已退出容器的命令。
命令:
$ docker ps -a
输出:
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
在上面的状态列下的输出中,你可以注意到前两个容器没有运行,而最后一个容器正在运行。
列出 Docker 中特定数量的容器
我们还可以在我们的系统中显示特定数量的容器。假设我们只想显示前两个容器。我们可以通过在上面使用的命令旁边使用 -n
标签来做到这一点。
这将同时显示 running 和 exited 但不超过指定的数量。
命令:
$ docker container ls -n 2
输出:
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
同样,使用 docker ps
命令还将列出我们在下面指定的容器的确切数量。以下是我们如何实现它。
命令:
$ docker ps -a -n 2
输出:
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
仅列出 Docker 中已退出的容器
使用过滤器,我们还可以仅列出当前未运行的容器。使用 -f
标签来指定我们想要满足的条件,在这种情况下,我们只希望返回具有退出状态的容器。
如果我们希望基于其他内容返回容器,我们还可以添加多个过滤器。这是我们如何列出具有退出状态的容器的方法。
命令:
$ docker container ls --filter "status=exited"
输出:
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
在 Docker 中列出最近创建的容器
也可以使用 -latest
标签返回最新的容器。这将返回我们最近创建的当前容器。
命令:
$ docker container ls --latest
输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 55 minutes ago Exited (0) 53 minutes ago epic_jackson
在 Docker 中按照 ID 列出容器
我们还可以根据容器的 ID 列出容器,我们可以使用 -q
来做到这一点,也称为 quiet 选项,如下所示。
命令:
$ docker container ls -q
输出:
d780996c499a
f7509cd49142
72c8debe5efa
在 Docker 中列出容器及其大小
我们还可以在容器大小旁边列出容器。我们可以确定那些占用最大内存大小的人,以及其他细节。
我们可以通过使用 -s
标签来实现这一点,也称为尺寸标签。
命令:
$ docker container ls -s
输出:
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)
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类: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
发布时间: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