docker compose ps
| Description | List containers |
|---|---|
| Usage | 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)
选项
| Option | Default | Description |
|---|---|---|
-a, --all | Show all stopped containers (including those created by the run command) | |
--filter | Filter services by a property (supported filters: status) | |
--format | table | Format output using a custom template: 'table': Print output in table format with column headers (default) 'table TEMPLATE': Print output in table format using the given Go template 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
--no-trunc | Don't truncate output | |
--orphans | true | Include orphaned services (not declared by project) |
-q, --quiet | Only display IDs | |
--services | Display services | |
--status | Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited] |
示例
格式化输出 (--format)
默认情况下,docker compose ps 命令使用表格(“pretty”)格式来显示容器。--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)
--status 标志 是 --filter 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= 选项,但未来可能会添加其他过滤选项。