查看容器的几种方式

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仅显示容器idOnly 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
.CommandQuoted 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}}'
docker 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.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Docker容器可以处于以下几种状态: 1. Created(已创建):当你使用`docker create`命令创建一个容器时,容器处于已创建状态。在这个阶段,Docker引擎会为容器分配资源并准备运行环境,但容器尚未启动。 2. Running(运行中):当使用`docker start`命令启动一个已创建的容器时,容器进入运行状态。在这个状态下,容器的进程正在后台运行,并且可以通过Docker引擎进行管理和监控。 3. Paused(已暂停):通过`docker pause`命令,可以将运行中的容器暂停。在暂停状态下,容器的所有进程都被冻结,但容器的状态保持不变。这个状态可以通过`docker unpause`命令解除。 4. Restarting(重启中):当容器正在重新启动时,它将进入重启状态。这可能是由于容器的配置更改或容器进程的异常终止导致的。 5. Exited(已退出):当容器中的主要进程终止或退出时,容器将进入已退出状态。这可能是由于进程正常退出、执行完毕或异常终止等原因引起的。在这个状态下,容器不再运行,但其文件系统和资源仍然存在。 6. Dead(已停止):如果Docker引擎检测到容器的主要进程无法启动或异常终止,容器将进入已停止状态。这个状态表示容器无法正常运行,并且需要修复或重新创建。 需要注意的是,上述状态仅涉及容器本身,而不包括容器所在的宿主机状态。此外,容器状态的具体名称和描述可能在不同的Docker版本中有所变化,但概念和原则保持一致。你可以使用`docker ps`命令查看当前活动的容器及其状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星光落入你灰蒙蒙的眼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值