查看容器的几种方式

docker ps 命令

hub.docker官网

参数 作用 系统解释
-a,--all 展现出来所有状态的容器 Show all containers (default shows just running)
-f,--filter 过滤显示 Filter output based on conditions provided
--format 格式化显示 Pretty-print containers using a Go template
-n,--last 显示n个最近创建的容器(包括所有状态) Show n last created containers (includes all states) (default -1)
-l, --latest 显示最新创建的容器(包括所有状态) Show the latest created container (includes all states)
--no-trunc 不要截断输出 Don’t truncate output
-q, --quiet 仅显示容器id Only display container IDs
-s, --size 显示总文件大小 Display total file sizes

[root@localhost ~]# docker ps #查看当前正在运行的容器
[root@localhost ~]# docker ps -a #查看所有运行过的容器
[root@localhost ~]# docker ps -aq
5aac14704d34(12位长度短ID)
49fcfe375189
[root@localhost ~]# docker ps -s For more information, refer to the container size on disk section.
SIZE 0B (virtual 204MB) 容器虚拟大小 = 容器真实增加大小 + 容器镜像大小

“size”			显示用于每个容器的可写层的数据量(在磁盘上)
“virtual size”	用于容器和可写层使用的只读image数据的磁盘空间总量

也可以使用 docker container ls 命令来观察当前系统正在运行的容器列表。

-f 带有参数进行过滤

过滤一个条件就加一个–filter,也就是有几个条件命令行里面就得有几个 --filter。
过滤标志(-f或--filter)格式是一个key=value对。如果有多个过滤器,则传递多个标志(例如 --filter “foo = bar” --filter “bif=baz”)

例如:docker ps -a --filter 条件1 --filter 条件2 --filter 条件3

☑ 条件支持正则,例如^以什么开头,$以什么结尾

☑ 过滤条件是以键值对的方式在后面列出来的,例如:status= exited
目前已知的条件

Filter								Description
id									容器ID
name								容器名字
label					表示键或键值对的任意字符串。表示为< key >< key>=<value >
exited							表示容器退出代码的整数。只对--all有用
status			     One of created, restarting, running, removing, paused, exited, or dead
ancestor		     过滤共享给定image作为原始的容器。 Expressed as <image-name>[:<tag>], <image id>, or <image@digest>
before or since			过滤在给定容器ID或名称之前或之后创建的容器
volume	 				过滤运行已装载给定卷或绑定装载的容器
network	 				过滤连接到给定网络的运行容器
publish or expose		过滤发布或公开给定端口的容器。 Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
health					根据容器的运行状况检查状态过滤容器。 One of starting, healthy, unhealthy or none.
isolation				仅限Windows守护程序。 One of default, process, or hyperv.
is-task					过滤作为服务“task”的容器。 Boolean option (true or false)

[root@localhost ~]# docker ps -f name=^web #过滤以web开头的容器
[root@localhost ~]# docker ps -f name=eb2$ #过滤以eb2结尾的容器
[root@localhost ~]# docker ps -f status=exited #过滤状态是exit的容器
条件虽多,但万变不离其宗,只要再记住以下 3 条准则:

  1. 选项后跟的都是键值对 key=value (可不带引号),如果有多个过滤条件,就多次使用 filter 选项。例如:
docker ps --filter id=abc --filter name=mysql
  1. 相同条件之间的关系是或,不同条件之间的关系是与。例如:
docker ps --filter name=mysql --filter name=redis --filter status=running

以上过滤条件会找出 name 包含 mysql 或 redis 并且 status 为 running 的容器。

  1. id 和 name,支持正则表达式,使用起来非常灵活。例如:
docker ps --filter name=^/my$

精确匹配 name 为 my 的容器。注意,容器实际名称,开头是有一个正斜线 / ,可用 docker inspect 一看便知。

docker ps --filter name=.*qq.*

匹配 name 包含 qq 的容器,和 --filter name=qq 一个效果。

最后, 举一个复杂点的例子,用于清理名称包含 redis,且状态为 exited 或 dead 的容器:

docker rm $(docker ps -q --filter name=.*redis.* --filter status=exited --filter status=dead)

format 使用Go模板漂亮地打印容器输出

可选项前面的.别落下了哈

- -format可选 功能显示
.ID 容器ID
.Image 镜像ID
.Command Quoted command
.CreatedAt 创建容器的时间
.RunningFor 自容器启动以来经过的时间
.Ports 暴露的端口
.State 容器状态 (for example; “created”, “running”, “exited”)
.Status 包含持续时间和健康状况详细信息的容器状态
.Size 容器磁盘大小
.Names 容器名称
.Labels 分配给容器的所有标签
.Label 此容器的特定标签的值 For example ‘{ {.Label “com.docker.swarm.cpu”}}’
.Mounts 该容器中装入的卷的名称
.Networks 连接到此容器的网络的名称

举例说明
当使用--format选项时,ps命令要么完全按照模板声明的方式输出数据,要么在使用table指令时也包括列标题。 以下示例使用不带标题的模板,并为所有正在运行的容器输出由冒号(:)分隔的ID和命令条目:

docker ps --format "{
    
    {.ID}}: {
    
    {.Command}}"

要以表格格式列出所有正在运行的容器及其标签,您可以使用:

docker ps --format "table {
    
    {.ID}}\t{
    
    {.Names}}\t{
    
    {.Ports}}"

docker container

移除所有停止的容器

# docker container prune [OPTIONS]
--filter		    提供过滤器值(例如,“until=<timestamp>)
--force , -f		不提示确认

Filtering

The filtering flag (--filter) format is of “key=value”. If there is more than one filter, then pass multiple flags (e.g., --filter “foo=bar” --filter “bif=baz”)
▷ 目前支持的过滤器有:
until (<timestamp>) 仅删除在给定时间戳之前创建的容器
label (label=<key>, label=<key>=<value>, label!=<key>, or label!=<key>=<value>) 仅删除容器具有或者不具有, in case label!=… is used) 指定标签
这个过滤器可以是Unix时间戳、日期格式的时间戳或相对于守护程序机器的时间计算的Go持续时间字符串(例如10m、1h30m)。日期格式时间戳支持的格式包括RFC3339Nano, RFC3339, 2006-01-02T15:04:05, 2006-01-02T15:04:05.999999999, 2006-01-02Z07:00, and 2006-01-02. The local timezone on the daemon will be used if you do not provide either a Z or a ±00:00 timezone offset at the end of the timestamp. 当提供Unix时间戳时,输入秒[纳秒], 其中seconds是自1月1日以来经过的秒数, 1970 (midnight UTC/GMT), not counting leap seconds (aka Unix epoch or Unix time),和可选的。纳秒字段是一秒的几分之一,长度不超过九位数。

▷ 标签过滤器接受两种格式:
One is the label=… (label= or label==) 它移除带有指定标签的容器。
The other format is the label!=… (label!= or label!==) 它移除没有指定标签的容器。
以下内容删除5分钟前创建的容器:

docker ps -a --format 'table {
    
    {.ID}}\t{
    
    {.Image}}\t{
    
    {.Command}}\t{
    
    {.CreatedAt}}\t{
    
    {.Status}}'
ocker container prune --force --filter "until=5m"

以下内容删除2017-01-04T13:10:00之前创建的容器:

docker container prune --force --filter "until=2017-01-04T13:10:00"

It’s too crowded halfway up the mountain. I have to go to the top.

猜你喜欢

转载自blog.csdn.net/qq_50573146/article/details/125986763