docker 撰写 ps
描述 | 列出容器 |
---|---|
用法 | docker compose ps [OPTIONS] [SERVICE...] |
描述
列出 Compose 项目的容器,以及当前状态和公开的端口。
$ docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
example-foo-1 alpine "/entrypoint.…" foo 4 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp
默认情况下,仅显示正在运行的容器。--all
标志可用于包含已停止的容器。
$ docker compose ps --all
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
example-foo-1 alpine "/entrypoint.…" foo 4 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp
example-bar-1 alpine "/entrypoint.…" bar 4 seconds ago exited (0)
选项
选项 | 默认 | 描述 |
---|---|---|
-a, --all | 显示所有已停止的容器(包括由 run 命令创建的容器) | |
--filter | 按属性过滤服务(支持的过滤器:状态) | |
--format | table | 使用自定义模板设置输出格式: 'table':以带有列标题的表格格式打印输出(默认) 'table TEMPLATE':使用给定的 Go 模板以表格格式打印输出 'json':以 JSON 格式打印 'TEMPLATE':打印使用给定的 Go 模板输出。有关使用模板格式化输出的更多信息, 请参阅 https://docker.github.net.cn/go/formatting/ |
--no-trunc | 不要截断输出 | |
--orphans | true | 包括孤立服务(项目未声明) |
-q, --quiet | 只显示ID | |
--services | 显示服务 | |
--status | 按状态过滤服务。值:[暂停|重新启动 |删除 |运行|死了|创建 |退出] |
例子
格式化输出(--format)
默认情况下,该docker compose ps
命令使用表格(“漂亮”)格式来显示容器。该--format
标志允许您指定输出的替代表示形式。目前,支持的选项有pretty
(默认)和json
,它将有关容器的信息作为 JSON 数组输出:
$ docker compose ps --format json
[{"ID":"1553b0236cf4d2715845f053a4ee97042c4f9a2ef655731ee34f1f7940eaa41a","Name":"example-bar-1","Command":"/docker-entrypoint.sh nginx -g 'daemon off;'","Project":"example","Service":"bar","State":"exited","Health":"","ExitCode":0,"Publishers":null},{"ID":"f02a4efaabb67416e1ff127d51c4b5578634a0ad5743bd65225ff7d1909a3fa0","Name":"example-foo-1","Command":"/docker-entrypoint.sh nginx -g 'daemon off;'","Project":"example","Service":"foo","State":"running","Health":"","ExitCode":0,"Publishers":[{"URL":"0.0.0.0","TargetPort":80,"PublishedPort":8080,"Protocol":"tcp"}]}]
JSON 输出允许您使用其他工具中的信息进行进一步处理,例如,使用
jq
实用程序
漂亮地打印 JSON:
$ docker compose ps --format json | jq .
[
{
"ID": "1553b0236cf4d2715845f053a4ee97042c4f9a2ef655731ee34f1f7940eaa41a",
"Name": "example-bar-1",
"Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
"Project": "example",
"Service": "bar",
"State": "exited",
"Health": "",
"ExitCode": 0,
"Publishers": null
},
{
"ID": "f02a4efaabb67416e1ff127d51c4b5578634a0ad5743bd65225ff7d1909a3fa0",
"Name": "example-foo-1",
"Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
"Project": "example",
"Service": "foo",
"State": "running",
"Health": "",
"ExitCode": 0,
"Publishers": [
{
"URL": "0.0.0.0",
"TargetPort": 80,
"PublishedPort": 8080,
"Protocol": "tcp"
}
]
}
]
按状态过滤容器 (--status)
使用该--status
标志按状态过滤容器列表。例如,仅显示正在运行的容器或仅显示已退出的容器:
$ docker compose ps --status=running
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
example-foo-1 alpine "/entrypoint.…" foo 4 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp
$ docker compose ps --status=exited
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
example-bar-1 alpine "/entrypoint.…" bar 4 seconds ago exited (0)
按状态过滤容器 (--filter)
flag
是--status
flag的方便简写--filter status=<status>
。下面的示例与上一节中的示例等效,这次使用了标志--filter
:
$ docker compose ps --filter status=running
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
example-foo-1 alpine "/entrypoint.…" foo 4 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp
该docker compose ps
命令当前仅支持该--filter status=<status>
选项,但将来可能会添加其他过滤器选项。