Docker命令行参考(17) – docker events从服务器获取实时事件

有运维或运维开发方面的需求,可以联系博主QQ 452336092或Email:admin#centos.bz(收费)

文章目录
[隐藏]
  1. Usage:  docker events [OPTIONS]
  2.  
  3. Get real time events from the server
  4.  
  5. Options:
  6.   -f, --filter value   Filter output based on conditions provided (default [])
  7.       --help           Print usage
  8.       --since string   Show all events created since timestamp
  9.       --until string   Stream events until this timestamp

Docker容器报告如下事件:

  1. attach, commit, copy, create, destroy, detach, die, exec_create, exec_detach, exec_start, export, health_status, kill, oom, pause, rename, resize, restart, start, stop, top, unpause, update

Docker镜像报告如下事件:

  1. delete, import, load, pull, push, save, tag, untag

Docker插件(实验)报告如下事件:

  1. install, enable, disable, remove

Docker数据卷报告如下事件:

  1. create, mount, unmount, destroy

Docker网络报告如下事件:

  1. create, connect, disconnect, destroy

Docker daemon报告如下事件:

  1. reload

–since和–until参数可以是与客户端机器时间相对的Unix时间戳,日期格式时间戳或Go duration字符串(如10m,1h30m)。如果没有提供–since选项,这个命令将只返回新的和/或实时事件。支持日期格式时间戳的格式包括RFC3339Nano, RFC3339, 2006-01-02T15:04:05, 2006-01-02T15:04:05.999999999, 2006-01-02Z07:00和2006-01-02。如果你没有在时间戳后提供时区Z或+-00:00,那么就使用客户端的本地时区。

过滤

过滤标志(-f或–filter)格式是“key=value”。如果要使用多个过滤,传递多个标志(如–filter “foo=bar” –filter “bif=baz”)。
使用相同的过滤多次将处理为OR;例如–filter container=588a23dac085 –filter container=a8f7720b8c22将显示588a23dac085或a8f7720b8c22的容器事件。
使用多个过滤将处理为AND;例如–filter container=588a23dac085 –filter event=start将显示容器为588a23dac085且事件类型为start的事件。
目前支持的过滤有:

  • container (container=)
  • event (event=)
  • image (image=)
  • plugin (experimental) (plugin=)
  • label (label= or label==)
  • type (type=)
  • volume (volume=)
  • network (network=)
  • daemon (daemon=)
  • 示例

    需要启两个shell来演示示例。

    Shell 1:监听事件

    1. $ docker events

    Shell 2:启动和停止容器

    1. $ docker start 4386fb97867d
    2. $ docker stop 4386fb97867d
    3. $ docker stop 7805c1d35632

    Shell 1: 将显示刚才的事件

    1. 2015-05-12T11:51:30.999999999Z07:00 container start 4386fb97867d (image=ubuntu-1:14.04)
    2. 2015-05-12T11:51:30.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    3. 2015-05-12T15:52:12.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    4. 2015-05-12T15:53:45.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    5. 2015-05-12T15:54:03.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)

    显示从指定时间的过去事件

    1. $ docker events --since 1378216169
    2. 2015-05-12T11:51:30.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    3. 2015-05-12T15:52:12.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    4. 2015-05-12T15:53:45.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    5. 2015-05-12T15:54:03.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)
    6.  
    7. $ docker events --since '2013-09-03'
    8. 2015-05-12T11:51:30.999999999Z07:00 container start 4386fb97867d (image=ubuntu-1:14.04)
    9. 2015-05-12T11:51:30.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    10. 2015-05-12T15:52:12.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    11. 2015-05-12T15:53:45.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    12. 2015-05-12T15:54:03.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)
    13.  
    14. $ docker events --since '2013-09-03T15:49:29'
    15. 2015-05-12T11:51:30.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    16. 2015-05-12T15:52:12.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    17. 2015-05-12T15:53:45.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    18. 2015-05-12T15:54:03.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)

    以下示例输出在最近3分钟内生成的所有事件,与客户端机器当前时间相关。

    1. $ docker events --since '3m'
    2. 2015-05-12T11:51:30.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    3. 2015-05-12T15:52:12.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    4. 2015-05-12T15:53:45.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    5. 2015-05-12T15:54:03.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)

    过滤事件

    1. $ docker events --filter 'event=stop'
    2. 2014-05-10T17:42:14.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    3. 2014-09-03T17:42:14.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)
    4.  
    5. $ docker events --filter 'image=ubuntu-1:14.04'
    6. 2014-05-10T17:42:14.999999999Z07:00 container start 4386fb97867d (image=ubuntu-1:14.04)
    7. 2014-05-10T17:42:14.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    8. 2014-05-10T17:42:14.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    9.  
    10. $ docker events --filter 'container=7805c1d35632'
    11. 2014-05-10T17:42:14.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    12. 2014-09-03T15:49:29.999999999Z07:00 container stop 7805c1d35632 (image= redis:2.8)
    13.  
    14. $ docker events --filter 'container=7805c1d35632' --filter 'container=4386fb97867d'
    15. 2014-09-03T15:49:29.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    16. 2014-05-10T17:42:14.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    17. 2014-05-10T17:42:14.999999999Z07:00 container die 7805c1d35632 (image=redis:2.8)
    18. 2014-09-03T15:49:29.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)
    19.  
    20. $ docker events --filter 'container=7805c1d35632' --filter 'event=stop'
    21. 2014-09-03T15:49:29.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)
    22.  
    23. $ docker events --filter 'container=container_1' --filter 'container=container_2'
    24. 2014-09-03T15:49:29.999999999Z07:00 container die 4386fb97867d (image=ubuntu-1:14.04)
    25. 2014-05-10T17:42:14.999999999Z07:00 container stop 4386fb97867d (image=ubuntu-1:14.04)
    26. 2014-05-10T17:42:14.999999999Z07:00 container die 7805c1d35632 (imager=redis:2.8)
    27. 2014-09-03T15:49:29.999999999Z07:00 container stop 7805c1d35632 (image=redis:2.8)
    28.  
    29. $ docker events --filter 'type=volume'
    30. 2015-12-23T21:05:28.136212689Z volume create test-event-volume-local (driver=local)
    31. 2015-12-23T21:05:28.383462717Z volume mount test-event-volume-local (read/write=true, container=562fe10671e9273da25eed36cdce26159085ac7ee6707105fd534866340a5025, destination=/foo, driver=local, propagation=rprivate)
    32. 2015-12-23T21:05:28.650314265Z volume unmount test-event-volume-local (container=562fe10671e9273da25eed36cdce26159085ac7ee6707105fd534866340a5025, driver=local)
    33. 2015-12-23T21:05:28.716218405Z volume destroy test-event-volume-local (driver=local)
    34.  
    35. $ docker events --filter 'type=network'
    36. 2015-12-23T21:38:24.705709133Z network create 8b111217944ba0ba844a65b13efcd57dc494932ee2527577758f939315ba2c5b (name=test-event-network-local, type=bridge)
    37. 2015-12-23T21:38:25.119625123Z network connect 8b111217944ba0ba844a65b13efcd57dc494932ee2527577758f939315ba2c5b (name=test-event-network-local, container=b4be644031a3d90b400f88ab3d4bdf4dc23adb250e696b6328b85441abe2c54e, type=bridge)
    38.  
    39. $ docker events --filter 'type=plugin' (experimental)
    40. 2016-07-25T17:30:14.825557616Z plugin pull ec7b87f2ce84330fe076e666f17dfc049d2d7ae0b8190763de94e1f2d105993f (name=tiborvass/no-remove:latest)
    41. 2016-07-25T17:30:14.888127370Z plugin enable ec7b87f2ce84330fe076e666f17dfc049d2d7ae0b8190763de94e1f2d105993f (name=tiborvass/no-remove:latest)

    转载请保留原文链接:Linux运维日志 » Docker命令行参考(17) – docker events从服务器获取实时事件

    打赏

    如果此文对你有所帮助,请随意打赏鼓励作者^_^