美文网首页
docker PS命令详解

docker PS命令详解

作者: _Gaara_ | 来源:发表于2020-08-11 09:12 被阅读0次

官网给的选项如下

Name, shorthand Default Description
--all , -a      Show all containers (default shows just running)
--filter , -f       Filter output based on conditions provided
--format        Pretty-print containers using a Go template
--last , -n -1  Show n last created containers (includes all states)
--latest , -l       Show the latest created container (includes all states)
--no-trunc      Don’t truncate output
--quiet , -q        Only display numeric IDs
--size , -s     Display total file sizes

docker ps 就是最基本的查询当前容器的命令,查询结果是所有start状态的容器

docker ps -a 查询全部状态的容器

docker ps -s 是查询包括容器大小的,start状态的容器。其中一个是容器真实增加的大小,一个是整个容器的虚拟大小。

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS       PORTS   NAMES        SIZE                                                                                      SIZE
e90b8831a4b8   nginx          "/bin/bash -c 'mkdir "   11 weeks ago   Up 4 hours           my_nginx     35.58 kB (virtual 109.2 MB)
00c6131c5e30   telegraf:1.5   "/entrypoint.sh"         11 weeks ago   Up 11 weeks          my_telegraf  0 B (virtual 209.5 MB)

docker ps --filter 是查询的时候加一个过滤,具体选项如下

Filter  Description
id  Container’s ID
name    Container’s name
label   An arbitrary string representing either a key or a key-value pair. Expressed as <key> or <key>=<value>
exited  An integer representing the container’s exit code. Only useful with --all.
status  One of created, restarting, running, removing, paused, exited, or dead
ancestor    Filters containers which share a given image as an ancestor. Expressed as <image-name>[:<tag>], <image id>, or <image@digest>
before or since Filters containers created before or after a given container ID or name
volume  Filters running containers which have mounted a given volume or bind mount.
network Filters running containers connected to a given network.
publish or expose   Filters containers which publish or expose a given port. Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
health  Filters containers based on their healthcheck status. One of starting, healthy, unhealthy or none.
isolation   Windows daemon only. One of default, process, or hyperv.
is-task Filters containers that are a “task” for a service. Boolean option (true or false)

举个例子:

$ docker ps --filter "label=color"

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
673394ef1d4c        busybox             "top"               47 seconds ago      Up 45 seconds                           nostalgic_shockley
d85756f57265        busybox             "top"               52 seconds ago      Up 51 seconds                           high_albattani
  1. 选项后跟的都是键值对 key=value (可不带引号),如果有多个过滤条件,就多次使用 filter 选项。例如:
docker ps --filter id=a1b2c3 --filter name=bingohuang
  1. 相同条件之间的关系是或,不同条件之间的关系是与。例如:
docker ps --filter name=bingo --filter name=huang --filter status=running

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

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

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

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

匹配 name 包含 demodocker 的容器,和 --filter name=demodocker 一个效果。
举一个复杂点的例子,用于清理名称包含 demodocker,且状态为 exited 或 dead 的容器:

docker rm $(docker ps -q --filter name=.*demodocker.* --filter status=exited --filter status=dead2>/dev/null)

4.可以通过状态进行筛查

$ docker ps --filter status=paused

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
673394ef1d4c        busybox             "top"               About an hour ago   Up About an hour (Paused)     
$ docker ps --filter status=running

CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
715ebfcee040        busybox                "top"               16 minutes ago      Up 16 minutes                           i_am_nostalgic
d5c976d3c462        busybox                "top"               23 minutes ago      Up 23 minutes                           top
9b6247364a03        busybox                "top"               24 minutes ago      Up 24 minutes                   
$ docker ps --filter ancestor=ubuntu

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
919e1179bdb8        ubuntu-c1           "top"               About a minute ago   Up About a minute                       admiring_lovelace
5d1e4a540723        ubuntu-c2           "top"               About a minute ago   Up About a minute                       admiring_sammet

简单来说就是个查询筛选,条件就是上面给出的选项,自行自由选用。

docker ps format

顾名思义就是个规范查询结果使用的命令,可以与filter搭配使用
示例用法如下:

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

a87ecb4f327c: /bin/sh -c #(nop) MA
01946d9d34d8: /bin/sh -c #(nop) MA
c1d3b0166030: /bin/sh -c yum -y up
41d50ecd2f57: /bin/sh -c #(nop) MA
$ docker ps --format "table {{.ID}}\t{{.Labels}}"

CONTAINER ID        LABELS
a87ecb4f327c        com.docker.swarm.node=ubuntu,com.docker.swarm.storage=ssd
c1d3b0166030        com.docker.swarm.node=debian,com.docker.swarm.cpu=6
$ docker ps --format "{{.ID}} : {{.Names}}"
0be32bebc1f1 : education
d4bed1132dbc : mysql8.0
db704298a586 : billplus
227f2253ac6d : Redis
32a01b1702f0 : MySql5.7

官方给出的可选项如下

Placeholder Description
.ID Container ID
.Image  Image ID
.Command    Quoted command
.CreatedAt  Time when the container was created.
.RunningFor Elapsed time since the container was started.
.Ports  Exposed ports.
.Status Container status.
.Size   Container disk size.
.Names  Container names.
.Labels All labels assigned to the container.
.Label  Value of a specific label for this container. For example '{{.Label "com.docker.swarm.cpu"}}'
.Mounts Names of the volumes mounted in this container.
.Networks   Names of the networks attached to this container.

相关文章

网友评论

      本文标题:docker PS命令详解

      本文链接:https://www.haomeiwen.com/subject/aeyfdktx.html