docker1.12中目前有48条命令,这里面有非常简单的version或者info的确认信息用的命令,也有内置的swarm mode的需要有集群环境才能执行的命令。我们在这个系列中将会逐条确认简单的使用方法。
这篇文章中学习一下各种不具有破坏性的查询信息的命令

事前准备

OS

演示用Linux发型版使用CentOS7.2,Linux内核3.10

[root@liumiaocn ~]# uname -a
Linux liumiaocn 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@liumiaocn ~]#

Docker版本

Docker版本1.12.1
CentOS7上安装Docker1.12.1http://blog.csdn.net/liumiaocn/article/details/52130852

11条查询系命令

以下是docker的11条查询命令,我们会在接下来的例子中逐条确认使用方法。

项番命令详解
No.11historyShow the history of an image
No.12imagesList images
No.14infoDisplay system-wide information
No.15inspectReturn low-level information on a container, image or task
No.20logsFetch the logs of a container
No.25portList port mappings or a specific mapping for the container
No.26psList containers
No.35searchSearch the Docker Hub for images
No.39statsDisplay a live stream of container(s) resource usage statistics
No.43topDisplay the running processes of a container
No.46versionShow the Docker version information

history

history用于确认image的履历信息. 用于刨根问底确认这个images最近都改了什么非常有效

[root@liumiaocn ~]# docker history minio/minio
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
8f7b277a8ac3        8 days ago          /bin/sh -c #(nop)  CMD ["/export"]              0 B
<missing>           8 days ago          /bin/sh -c #(nop)  VOLUME [/export]             0 B
<missing>           8 days ago          /bin/sh -c #(nop)  ENTRYPOINT ["minio" "serve   0 B
<missing>           8 days ago          /bin/sh -c #(nop)  EXPOSE 9000/tcp              0 B
<missing>           8 days ago          /bin/sh -c apk add --no-cache git bash &&  go   22.19 MB
<missing>           8 days ago          /bin/sh -c #(nop) COPY dir:e929bdab8a57250ad4   7.33 MB
<missing>           8 days ago          /bin/sh -c #(nop)  ENV ALLOW_CONTAINER_ROOT=1   0 B
<missing>           8 days ago          /bin/sh -c #(nop)  WORKDIR /go/src/app          0 B
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:f6191f2c86edc9343   2.478 kB
<missing>           3 weeks ago         /bin/sh -c #(nop)  WORKDIR /go                  0 B
<missing>           3 weeks ago         /bin/sh -c mkdir -p "$GOPATH/src" "$GOPATH/bi   0 B
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV PATH=/go/bin:/usr/loca   0 B
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV GOPATH=/go               0 B
<missing>           3 weeks ago         /bin/sh -c set -ex  && apk add --no-cache --v   278.2 MB
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:b2d7156cdbff1193f   567 B
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV GOLANG_SRC_SHA256=6326   0 B
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV GOLANG_SRC_URL=https:/   0 B
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV GOLANG_VERSION=1.6.3     0 B
<missing>           3 weeks ago         /bin/sh -c apk add --no-cache ca-certificates   603.1 kB
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:852e9d0cb9d906535a   4.795 MB
[root@liumiaocn ~]#

images

查询当前机器的镜像列表,比如可以看出liumiaocn这台机器上目前只有2个镜像

[root@liumiaocn ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
minio/minio               latest              8f7b277a8ac3        8 days ago          313.1 MB
74th/redmine-all-in-one   latest              cbd38a5bb23d        7 months ago        1.515 GB
[root@liumiaocn ~]#

info

用于确认当前docker的系统信息,包括

  • 容器相关:个数/状态
  • Server版本号
  • Storage driver及详细信息
  • Logging driver格式
  • Kernel版本
  • OS类型
  • 当前机器的硬件信息
[root@liumiaocn ~]# docker info
Containers: 2
 Running: 0
 Paused: 0
 Stopped: 2
Images: 49
Server Version: 1.12.1
Storage Driver: devicemapper
 Pool Name: docker-253:0-458525-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 107.4 GB
 Backing Filesystem: ext4
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 4.134 GB
 Data Space Total: 107.4 GB
 Data Space Available: 40.53 GB
 Metadata Space Used: 5.808 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.142 GB
 Thin Pool Minimum Free Space: 10.74 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2015-10-14)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: null host bridge overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.939 GiB
Name: liumiaocn
ID: NZWQ:WON7:SYFE:2VY3:RIEM:HIKZ:262D:FPYQ:FQ5O:LATG:GC3D:ZS2P
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: true
Insecure Registries:
 127.0.0.0/8
[root@liumiaocn ~]#

inspect

用于检查容器或者镜像low-level的jason信息,比如:

[root@liumiaocn ~]# docker inspect minio/minio
[
    {
        "Id": "sha256:8f7b277a8ac363a089ea15df21a580a84efb3dea137a36038205e59325efa427",
        "RepoTags": [
            "minio/minio:latest"
        ],
        "RepoDigests": [
            "minio/minio@sha256:21dc25d1c23ed06a361b6558fd5c2600e411b8fc5d2a01c29f96edac10d62b0d"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2016-09-13T10:29:14.716574798Z",
        "Container": "f41f2c8f9a85781c64d8e7366930f61bbfcdde9c861dd80f226049322f840e48",
        "ContainerConfig": {
            "Hostname": "fd645bd7790b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "9000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOLANG_VERSION=1.6.3",
                "GOLANG_SRC_URL=https://golang.org/dl/go1.6.3.src.tar.gz",
                "GOLANG_SRC_SHA256=6326aeed5f86cf18f16d6dc831405614f855e2d416a91fd3fdc334f772345b00",
                "GOPATH=/go",
                "ALLOW_CONTAINER_ROOT=1"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/export\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:70c08539954d40592d08fde562fe5cafca6070fce64f316517f3aabbb66cfdb2",
            "Volumes": {
                "/export": {}
            },
            "WorkingDir": "/go/src/app",
            "Entrypoint": [
                "minio",
                "server"
            ],
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "1.12.0",
        "Author": "",
        "Config": {
            "Hostname": "fd645bd7790b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "9000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOLANG_VERSION=1.6.3",
                "GOLANG_SRC_URL=https://golang.org/dl/go1.6.3.src.tar.gz",
                "GOLANG_SRC_SHA256=6326aeed5f86cf18f16d6dc831405614f855e2d416a91fd3fdc334f772345b00",
                "GOPATH=/go",
                "ALLOW_CONTAINER_ROOT=1"
            ],
            "Cmd": [
                "/export"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:70c08539954d40592d08fde562fe5cafca6070fce64f316517f3aabbb66cfdb2",
            "Volumes": {
                "/export": {}
            },
            "WorkingDir": "/go/src/app",
            "Entrypoint": [
                "minio",
                "server"
            ],
            "OnBuild": [],
            "Labels": {}
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 313073347,
        "VirtualSize": 313073347,
        "GraphDriver": {
            "Name": "devicemapper",
            "Data": {
                "DeviceId": "61",
                "DeviceName": "docker-253:0-458525-a6060fc06804adb71b66288b99dff8c65b18a9048af34482b57a4bb0ea6eef37",
                "DeviceSize": "107374182400"
            }
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:4fe15f8d0ae69e169824f25f1d4da3015a48feeeeebb265cd2e328e15c6a869f",
                "sha256:c51757fa6b1f5b8205a245f92e7a8d2e9bb1e29c843dc3fdfff3008d59696b41",
                "sha256:b6f00c09f09f1c086cfe2f559eecff8de90bc35e5a9a17fb859445fba9915fe6",
                "sha256:08562565bb8c2e880c16b070d498e033fce7243a460f80f957fb6ee57e3e034c",
                "sha256:5e1680a086c141fa4e4ed9411fa2559447052093c8becc420a9a819d8e1dc741",
                "sha256:312bad5c05f55337dfce150dc248864744cb297b9c98ba6f944912ec075ab7c4",
                "sha256:cebdf3afd643b70adadc7c8ca3a5fb76a86665d98a83df5832947125c01bc1a3",
                "sha256:a410cb042a26fc93a1905b94eab38e34f51371435810d5ac1f963c8219cf2361",
                "sha256:fc637fd3d245f2ec17dcd1273f155e363a709c3c1ed7bd51ed0b65c2e9157b03",
                "sha256:742c6495d40600942ad95ca852280a8827e870159a9d1d3901a4fd7b48323a2f"
            ]
        }
    }
]
[root@liumiaocn ~]#

logs

查询container的log信息,确认了一下当前正在运行的container,发现没有,全部都exit了,exit后的也可以确认,
这是生产或者测试环境中停止后的docker进行状况确认的常见case。

[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
e7138f83f650        minio/minio         "minio server /export"   2 hours ago         Exited (0) 2 hours ago                        kickass_mahavira
770f57c8139c        cbd38a5bb23d        "/bin/sh -c 'sh /root"   6 months ago        Exited (0) 6 months ago                       grave_curie
[root@liumiaocn ~]#

停下的container也可以看到当时出过什么log

[root@liumiaocn ~]# docker logs e7138f83f650

Endpoint:  http://172.17.0.2:9000  http://127.0.0.1:9000
AccessKey: 5SQH62XUCB09E6NZDSB5
SecretKey: aGDbCYtcVQeObqk1aks6AsdTPekiMnx3Q8KbWKOs
Region:    us-east-1
SqsARNs:

Browser Access:
   http://172.17.0.2:9000  http://127.0.0.1:9000

Command-line Access: https://docs.minio.io/docs/minio-client-quickstart-guide
   $ mc config host add myminio http://172.17.0.2:9000 5SQH62XUCB09E6NZDSB5 aGDbCYtcVQeObqk1aks6AsdTPekiMnx3Q8KbWKOs

Object API (Amazon S3 compatible):
   Go:         https://docs.minio.io/docs/golang-client-quickstart-guide
   Java:       https://docs.minio.io/docs/java-client-quickstart-guide
   Python:     https://docs.minio.io/docs/python-client-quickstart-guide
   JavaScript: https://docs.minio.io/docs/javascript-client-quickstart-guide
[root@liumiaocn ~]#

port

是为了确认port的mapping状况,需要container在live的状态才能确认到port情况

[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
e7138f83f650        minio/minio         "minio server /export"   2 hours ago         Exited (0) 2 hours ago                        kickass_mahavira
770f57c8139c        cbd38a5bb23d        "/bin/sh -c 'sh /root"   6 months ago        Exited (0) 6 months ago                       grave_curie
[root@liumiaocn ~]# docker port e7138f83f650
[root@liumiaocn ~]#

在容器e7138f83f650停止的状态下确认port,没有任何信息。然后将该容器启动

[root@liumiaocn ~]# docker start e7138f83f650
e7138f83f650
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                    NAMES
e7138f83f650        minio/minio         "minio server /export"   2 hours ago         Up 4 seconds              0.0.0.0:9000->9000/tcp   kickass_mahavira
770f57c8139c        cbd38a5bb23d        "/bin/sh -c 'sh /root"   6 months ago        Exited (0) 6 months ago                            grave_curie
[root@liumiaocn ~]#

对启动后的容器可以确认出相关port的mapping信息

[root@liumiaocn ~]# docker port e7138f83f650
9000/tcp -> 0.0.0.0:9000
[root@liumiaocn ~]#

ps

default的情况是列出活着的container

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
e7138f83f650        minio/minio         "minio server /export"   2 hours ago         Up 3 minutes        0.0.0.0:9000->9000/tcp   kickass_mahavira
[root@liumiaocn ~]#

ps -a是列出所有的container的信息,包括停止了的。

default的情况下搜索dockerhub上的镜像信息

[root@liumiaocn ~]# docker search minio/minio
NAME                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
minio/minio                 Minio is an Amazon S3 compatible object st...   14                   [OK]
zenithar/minio-server       Minio.io Server in Alpine Linux docker          1                    [OK]
minio/doctor                Doctor is a Documentation Server for all y...   1                    [OK]
mofon/minio-docker          minio                                           1                    [OK]
daspanel/minio-fileserver   Docker image that provides volumes to be s...   0                    [OK]
yoyowallet/minio            Minio is an object storage server compatib...   0                    [OK]
qnib/minio                  Docker image holding the minio object stor...   0                    [OK]
soxhub/minio                Minio is an object storage server compatib...   0                    [OK]
noumia/minio                Docker minio object storage server              0                    [OK]
priordev/minio              Minio S3 Storage Server                         0                    [OK]
insightsoftware/minio       This is a docker image with minio installe...   0
webhippie/minio             Docker images for minio                         0                    [OK]
benchflow/minio             BenchFlow Minio datastore                       0                    [OK]
faisyl/minio-fixed          Fix for minios official image till they re...   0                    [OK]
deisci/minio                                                                0
kmala/minio                                                                 0
kemchos/minio                                                               0                    [OK]
bigvikinggames/minio        Minio Cloud Storage                             0
smothiki/minio                                                              0
mattyr/minio                Minio is a object storage server written i...   0
aalpern/minio               Bleeding edge build of minio/minio              0
pmosbach/minio              Minio experiment for IPv4 only hosts            0
sych74/minio                                                                0
jarfil/salt-minion          Salt minion only                                0                    [OK]
deis/minio                                                                  0
[root@liumiaocn ~]#

stats

获取实时的container的资源用量的统计信息, 会不断自动刷新

#docker stats
CONTAINER           CPU %               MEM USAGE / LIMIT       MEM %               NET I/O              BLOCK I/O           PIDS
e7138f83f650        0.00%               19.74 MiB / 1.939 GiB   0.99%               7.613 kB / 4.15 kB   14.09 MB / 0 B      0

top

列出容器实际的进程详细信息

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
e7138f83f650        minio/minio         "minio server /export"   2 hours ago         Up 7 minutes        0.0.0.0:9000->9000/tcp   kickass_mahavira
[root@liumiaocn ~]# docker top e7138f83f650
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                10482               10467               0                   13:54               ?                   00:00:00            minio server /export
[root@liumiaocn ~]#
[root@liumiaocn ~]# ps -ef |grep 10482
root      10482  10467  0 13:54 ?        00:00:00 minio server /export
root      11522   3563  0 14:03 pts/1    00:00:00 grep --color=auto 10482
[root@liumiaocn ~]#

version

确认docker的版本信息

[root@liumiaocn ~]# docker version
Client:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:
 OS/Arch:      linux/amd64
 Experimental: true

Server:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:
 OS/Arch:      linux/amd64
 Experimental: true
[root@liumiaocn ~]#
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐