1. 程式人生 > >Docke 1.12 基礎篇:48條命令(2)

Docke 1.12 基礎篇:48條命令(2)

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

11條查詢繫命令

以下是docker的11條查詢命令,我們會在接下來的例子中逐條確認使用方法。

項番 命令 詳解
No.11 history Show the history of an image
No.12 images List images
No.14 info Display system-wide information
No.15 inspect Return low-level information on a container, image or task
No.20 logs Fetch the logs of a container
No.25 port List port mappings or a specific mapping for the container
No.26 ps List containers
No.35 search Search the Docker Hub for images
No.39 stats Display a live stream of container(s) resource usage statistics
No.43 top Display the running processes of a container
No.46 version Show the Docker version information

history

history用於確認image的履歷資訊. 用於刨根問底確認這個images最近都改了什麼非常有效

[[email protected] ~]# 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
[[email protected] ~]#

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型別
  • 當前機器的硬體資訊
[[email protected] ~]# 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
[[email protected] ~]#

inspect

用於檢查容器或者映象low-level的jason資訊,比如:

[root@liumiaocn ~]# docker inspect minio/minio
[
    {
        "Id": "sha256:8f7b277a8ac363a089ea15df21a580a84efb3dea137a36038205e59325efa427",
        "RepoTags": [
            "minio/minio:latest"
        ],
        "RepoDigests": [
            "minio/[email protected]: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

[[email protected] ~]# 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
[[email protected] ~]#

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的資訊,包括停止了的。

search

default的情況下搜尋dockerhub上的映象資訊

[[email protected] ~]# 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
[[email protected] ~]#

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 ~]#