Docker命令解读-七

title: Docker命令解读(七)
date: 2015-11-25 09:46:52
tags: docker

categories: Docker Commands

Docker命令解读系列文章将系统讲解Docker使用的命令,方便大家学习Docker的基本操作。在写这个系列文章的时候,主要参考了Docker官方的文档,有些内容是直接的翻译。原文档地址 Docker Docs
转载请注明出处

目录:
- history
- events
- stats
- info
- logs
- port
- inspect
- top

history

Usage: docker history [OPTIONS] IMAGE

Show the history of an image

  -H, --human=true     Print sizes and dates in human readable format
  --help=false         Print usage
  --no-trunc=false     Don't truncate output
  -q, --quiet=false    Only show numeric IDs

docker history用于查看镜像的历史信息。包括镜像的各层创建的时间,创建时的命令等。

events

Usage: docker events [OPTIONS]

Get real time events from the server

  -f, --filter=[]    Filter output based on conditions provided
  --help=false       Print usage
  --since=""         Show all events created since timestamp
  --until=""         Stream events until this timestamp

docker events命令用于查看Docker服务器中发生的实时事件。“事件”分为镜像的和容器的两种:
容器可以报告的事件有:

attach, commit, copy, create, destroy, die, exec_create, exec_start, export, kill, oom, pause, rename, resize, restart, start, stop, top, unpause

镜像可以报告的事件有:

delete, import, pull, push, tag, untag

--since--until标识用于指定需要报告的事件的时间范围,其值可以是UNIX时间戳,RFC3399定义的日期或者Golang的时间间隔字符串(30m1h30m等等)
如果没有指定--since标识,Docker将只会报告新产生的或正在进行的时间,就好像是从此刻开始监听。

--filter标识用于事件的筛选,使用它,用户可以只获得某一个容器的状态或某种类型的状态。其格式为:--filter “event=create”|“container=test”--filter标识可以使用多次。
以下事实需要特别注意:
- 当使用--filter多次但指定的类型的都是一样时,Docker将多个--filter之间认定为OR关系,例如:
bash
--filter container=588a23dac085 --filter container=a8f7720b8c22

会显示容器588a23dac085或容器a8f7720b8c22的事件信息。
- 如果使用--filter多次但指定不同的类型,则被认作的AND的关系,例如:
--filter container=588a23dac085 --filter event=start会报告容器588a23dac085的start事件。

使用--filter可以指定以下四种类型的事件:
- container container=
- event event=
- image image=
- label label=orlabel==

举例:
直接执行docker events,表示监听从现在起服务器上的所有可以报告的事件。当有事件发生时,就会显示在执行docker events的控制端下。
在控制台1运行docker events

$ docker events

另打开一个控制台2,运行如下命令:

$ docker start 4386fb97867d
$ docker stop 4386fb97867d
$ docker stop 7805c1d35632

控制台1就会输出相应的事件:

2014-05-10T17:42:14.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) start
2014-05-10T17:42:14.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) die
2014-05-10T17:42:14.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) stop
2014-05-10T17:42:14.999999999Z07:00 7805c1d35632: (from redis:2.8) die
2014-05-10T17:42:14.999999999Z07:00 7805c1d35632: (from redis:2.8) stop

stats

Usage: docker stats [OPTIONS] CONTAINER [CONTAINER...]

Display a live stream of one or more containers' resource usage statistics

  --help=false       Print usage
  --no-stream=false  Disable streaming stats and only pull the first result

docker stats命令用于显示一个或多个容器实时的资源使用情况,包括CPU,内存使用量,网络IO,磁盘IO等信息。
docker stats命令仅显示正在运行的running容器的实时资源使用情况,不能显示已经停止的容器的资源使用情况。
可以通过--no-stream标识关闭实时状态,仅显示当前时间点的状态。

info

docker info用于显示系统信息,主要有下面这些:

Containers: 1
Images: 32
Server Version: 1.9.0
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 34
 Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.19.0-33-generic
Operating System: Ubuntu 14.04.3 LTS
CPUs: 4
Total Memory: 7.493 GiB
Name: chy-station
ID: D3GB:PFOA:JJ2S:3RGI:7RHU:OUDR:C45L:JAMF:7PS7:PIEC:XPIL:T5TJ
WARNING: No swap limit support

logs

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

  -f, --follow=false        Follow log output
  --help=false              Print usage
  --since=                  Show logs since timestamp
  -t, --timestamps=false    Show timestamps
  --tail=all                Number of lines to show from the end of the logs

显示某个容器的日志信息,此命令仅支持使用json-filejournald日志驱动的容器。
默认只显示已经产生的日志,通过指定-f(--follow跟随)标识接着显示以后的日志。
--since标识通过指定一个时间戳显示从某个时间点到现在的日志信息。
--tail标识用来指定要显示的日志的行数,默认值是“all”,即显示所有。
使用-t(--timestamps)标识会在每一行日志加上时间戳。
关于docker logs的原理,这里又一篇很好的文章可以参考:「Allen 谈 Docker 系列」之 docker logs 实现剖析

port

docker port命令用于查看容器内所有内部端口与公共端口(宿主机端口)之间的映射关系,或根据某个内部端口查看对应的公共端口。
举例:

$ docker port test
7890/tcp -> 0.0.0.0:4321
9876/tcp -> 0.0.0.0:1234
$ docker port test 7890/tcp
0.0.0.0:4321

inspect

Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

Return low-level information on a container or image

  -f, --format=""         Format the output using the given go template
  --help=false            Print usage
  --type=container|image  Return JSON for specified type, permissible
                          values are "image" or "container"
  -s, --size=false        Display total file sizes if the type is container

返回与容器或镜像相关的配置信息的josn字符串。默认返回所有信息,也可以通过--format标识指定要返回的部分信息。关于--format格式的信息请参考:text/format
下面是常用的格式:

$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $INSTANCE_ID   返回IP地址
$ docker inspect --format='{{.LogPath}}' $INSTANCE_ID 返回保存log信息文件的地址

top

显示容器中正在运行的进程。

你可能感兴趣的:(Docker命令解读-七)