docker之ps命令大解析

学好这个命令,你会感觉非常的的心用手,尤其是在docker野容器清理的时候,方便快速的找到所需要的容器,再配合管道符,相干什么就可以干什么

docker ps --help

[root@localhost ~]# docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        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           Only display container IDs
  -s, --size            Display total file sizes

参数

作用

系统解释

-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,–list

简单理解,就是显示输出第n行的docker ps内容

Show n last created containers (includes all states) (default -1)

-l, --latest

相当于ps -n 1

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

实操解释

###docker ps 只会输出是up状态的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web3
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web2

###无论什么状态的容器都会输出
[root@localhost ~]# docker ps -a   
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS                   PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours               80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours               80/tcp    web3
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours               80/tcp    web2
91bb31b24e75   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Exited (0) 2 hours ago             web1
[root@localhost ~]# docker ps --all
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS                   PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours               80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours               80/tcp    web3
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours               80/tcp    web2
91bb31b24e75   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Exited (0) 2 hours ago             web1
[root@localhost ~]# 

###查看容器前几列
[root@localhost ~]# docker ps -n 2
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web3
[root@localhost ~]# docker ps -n 3
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web3
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web2

[root@localhost ~]# docker ps -l
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web4

###无截断的输出,也就是输出全部
[root@localhost ~]# docker ps --no-trunc
CONTAINER ID                                                       IMAGE      COMMAND                           CREATED       STATUS       PORTS     NAMES
154748d5a3bfcff91a86ab236d2f1917543a41dc4aaccb379d1534b8269b035e   nginx:v1   "./sbin/nginx -g 'daemon off;'"   2 hours ago   Up 2 hours   80/tcp    web4
026d9b1f23fef0d7b73953e93ffd77f0cd54a09c3c70b660f9f14071483dd1a9   nginx:v1   "./sbin/nginx -g 'daemon off;'"   2 hours ago   Up 2 hours   80/tcp    web3
42e2b0a3ebb2f92ce027f949d9fce77cbbd30ef9dfb83f093fc89820d3803eea   nginx:v1   "./sbin/nginx -g 'daemon off;'"   2 hours ago   Up 2 hours   80/tcp    web2

###值显示容器id
[root@localhost ~]#  docker ps -q
154748d5a3bf
026d9b1f23fe
42e2b0a3ebb2

###显示容器的大小
[root@localhost ~]#  docker ps -s
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES     SIZE
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web4      2B (virtual 415MB)
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web3      2B (virtual 415MB)
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web2      2B (virtual 415MB)

docker -f,–filter参数详解

这个大家可以理解为带有参数进行过滤

  • 注意点



  • 目前已知的条件









#过滤以web开头的容器
[root@localhost ~]# docker ps -f name=^web   
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web3
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web2

#过滤以eb2结尾的容器
[root@localhost ~]# docker ps -f name=eb2$
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS     NAMES
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Up 2 hours   80/tcp    web2

#过滤状态是exit的容器
[root@localhost ~]# docker ps -f status=exited
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS                   PORTS     NAMES
91bb31b24e75   nginx:v1   "./sbin/nginx -g 'da…"   2 hours ago   Exited (0) 2 hours ago             web1

####其余的大家可以自己尝试

docker --format

这个大家可以这样理解,这个命令可以改变docker ps命令输出的格式



–format可选

功能显示

.ID

容器ID

.Image

镜像ID

.Command

执行的命令

.CreatedAt

容器创建时间

.RunningFor

运行时长

.Ports

暴露的端口

.Status

容器状态

.Names

容器名称

.Label

分配给容器的所有标签

.Mounts

容器挂载的卷

.Networks

容器所用的网络名称

#开始我的表演
[root@localhost ~]# docker ps -a   #正常是这样输出到屏幕的
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS                   PORTS     NAMES
154748d5a3bf   nginx:v1   "./sbin/nginx -g 'da…"   9 hours ago   Up 9 hours               80/tcp    web4
026d9b1f23fe   nginx:v1   "./sbin/nginx -g 'da…"   9 hours ago   Up 9 hours               80/tcp    web3
42e2b0a3ebb2   nginx:v1   "./sbin/nginx -g 'da…"   9 hours ago   Up 9 hours               80/tcp    web2
91bb31b24e75   nginx:v1   "./sbin/nginx -g 'da…"   9 hours ago   Exited (0) 9 hours ago             web1
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"   #利用--format后,可以指定他的输出格式,变成这样输出,是不是很酷,剩下的可以自己去探究吧
CONTAINER ID   NAMES     PORTS
154748d5a3bf   web4      80/tcp
026d9b1f23fe   web3      80/tcp
42e2b0a3ebb2   web2      80/tcp
为了找个女朋友而奋斗:结束语